Home » Blog » Problem Solving MATLAB Part 1 Chapman Book

Problem Solving MATLAB Part 1 Chapman Book

Q-1. Answer the following questions for the array shown here.

problem-solving-part-1

(a) What is the size of array1?

(b) What is the value of array1(1,4)?

(c) What is the size and value of array1(:,1:2:5)?

(d) What is the size and value of array1([1 3],end)?

clc;
clear all;
close all;
 
array1 = [0.0 0.5 2.1 -3.5 6.0; 
    0.0 -1.1 -6.6 2.8 3.4; 2.1 0.1 0.3 -0.4 1.3; 1.1 5.1 0.0 1.1 -2.0];
 
% (a) What is the size of array1?
size_array1 = size(array1);
fprintf("Size Of Array1 %d By %d\n\n",size_array1);
 
% (b) What is the value of array1(1,4)?
value = array1(1,4);
fprintf("Value Of Array1(1,4) = %5f\n",value);
 
% (c) What is the size and value of array1(:,1:2:5)?
array2 = array1(:,1:2:5);
size_array2 = size(array2);
disp('Array2');
disp(array2);
fprintf("Size Of Array2 Is %d By %d\n\n",size_array2);
 
% (d) What is the size and value of array1([1 3],end)?
array3 = array1([1 3],end);
size_array3 = size(array3);
disp('Array3');
disp([array3]);
fprintf("Size Of Array3 Is %d By %d\n",size_array3);

Output

problem-solving-part-1

Q-2. Are the following MATLAB variable names legal or illegal? Why?

(a) dog1

(b) 1dog

(c) Do_you_know_the_way_to_san_jose

(d) _help

(e) What’s_up?

clc;
clear all;
close all;
 
dog1=1;
1dog=1;
Do_you_know_the_way_to_san_jose=1;
help=1;
What's_up? = 1;

Output

a –> valid(number can be in between or at last)

b –> invalid because in matlab variable name cannot start with number

c –> valid(_under scotch is valid)

d –> valid

e –> invalid because ‘s cannot be used

problem-solving-part-1

Q-3. Determine the size and contents of the following arrays. Note that the later arrays may depend on the definitions of arrays defined earlier in this exercise.

(a) a = 2:3:8;

(b) b = [a’ a’ a’];

(c) c = b(1:2:3,1:2:3);

(d) d = a + b(2,:);

(e) w = [zeros(1,3) ones(3,1)’ 3:5′];

(f ) b([1 3],2) = b([3 1],2);

(g) e = 1:-1:5;

clc;
clear all;
close all;
 
a = 2:3:8;
disp(' a = ')
disp(a);
fprintf("Size Of a Is %d by %d\n",size(a));
 
b = [a' a' a'];
disp(' b = ')
disp(b);
fprintf("Size Of b Is %d by %d\n",size(b));
 
c = b(1:2:3, 1:2:3);
disp(' c = ')
disp(c);
fprintf("Size Of c Is %d by %d\n",size(c));
 
d = a + b(2,:);
disp(' d = ')
disp(d);
fprintf("Size Of d Is %d by %d\n",size(d));
 
w = [zeros(1,3) ones(3,1)' 3:5'];
disp(' w = ')
disp(w);
fprintf("Size Of w Is %d by %d\n",size(w));
 
b([1 3],2) = b([3 1],2);
disp(' Updated b = ')
disp(b);
fprintf("Size Of Updated b Is %d by %d\n",size(b));
 
e = 1:-1:5;
disp(' e = ')
disp(e);
fprintf("Size Of e Is %d by %d\n\n",size(e));

Output

problem-solving-part-1problem-solving-part-1

Leave a Reply

Your email address will not be published.