Chapter 8 Exercises Answer Sheet

.pdf

School

Wharton County Junior College *

*We aren’t endorsed by this school

Course

2304

Subject

Computer Science

Date

May 1, 2024

Type

pdf

Pages

4

Uploaded by ElderSummerBoar45 on coursehero.com

Chapter 8 Exercises Answer Sheet For each problem, please insert your scripts, commands, and answers. Please save your completed work as a PDF file and submit it to Blackboard. 1) Create the following cell array: >> ca = {'abc', 11, 3:2:9, zeros(2)} Use the reshape function to make it a 2 x 2 matrix. Then, write an expression that would refer to just the last column of this cell array. Insert your answer here: >> ca = reshape(ca,2,2); >> ca{:,2} 2) Create a row vector cell array to store the character vector ‘xyz’, the number 33.3, the vector 2:6, and the logical expression ‘a’ < ‘c’. Use the transpose operator to make this a column vector, and use reshape to make it a 2 x 2 matrix. Use celldisp to display all elements. Insert your answer here: >> mycell = {'xyz',33.3,2:6,'a' < 'c'} mycell = 1×4 cell array {'xyz'} {[33.3000]} {1×5 double} {[1]} >> mycell = mycell' mycell = 4×1 cell array {'xyz' } {[ 33.3000]} {1×5 double} {[ 1]} >> mycell = reshape(mycell,2,2) mycell = 2×2 cell array {'xyz' } {1×5 double} {[33.3000]} {[ 1]} >> celldisp(mycell) mycell{1,1} = xyz mycell{2,1} = 33.3000 mycell{1,2} = 2 3 4 5 6 mycell{2,2} = 1 3) Create a cell array that stores phrases, such as: exclaimcell = {'Bravo', 'Fantastic job'}; Pick a random phrase to print. Insert your answer here:
>> ranindex = randi([1, length(exclaimcell)]); >> fprintf('%s\n', exclaimcell{ranindex}) 4) Write a script that will prompt the user for character vectors and read them in, store them in a cell array (in a loop), and then print them out. Insert your answer here: Ch8Ex4.m % Prompt the user for char vectors and store in a cell array for i = 1:4 str = input('Enter a character vector: ','s'); strcell{i} = str; end strcell 5) Create a cell array that contains character vectors, e.g., pets = {'cat', 'dog', 'snake'}; Show the difference in the following methods of indexing: pets(1:2) pets{1:2} [p1 p2] = pets{1:2} Insert your answer here: >> pets(1:2) ans = 1×2 cell array {'cat'} {'dog'} >> pets{1:2} ans = 'cat' ans = 'dog' >> [p1 p2] = pets{1:2} p1 = 'cat' p2 = 'dog' 6) Create a cell array variable that would store for a student his or her name, university id number, and GPA. Print this information. Insert your answer here: >> studentca= {'Smith, Susan', 12345678, 3.5}; >> fprintf('Name: %s\nUID: %d\nGPA: %.2f\n', studentca{1}, ... studentca{2}, studentca{3}) Name: Smith, Susan UID: 12345678 GPA: 3.50
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help