Construct the following array in Matlab. A = [5, 8, 12, 15, 13; 11, 15, 4, 9, 4;
ID: 3794907 • Letter: C
Question
Construct the following array in Matlab.
A = [5, 8, 12, 15, 13; 11, 15, 4, 9, 4; 10, 6, 8, 3, 13; 3, 9, 11, 3, 4; 2, 4, 14, 4, 14]
For the above array, use the find function and indexing to locate all elements in A that satisfy the following conditions. Note that find returns locations of the array elements that meet a condition rather than their actual values. What we are looking for here is the actual elements, not the location of the elements.
a) use the find function to locate all of the elements in A less than 9.
b) use the find function to locate all the elements that are less than 9 AND greater than 2. c) find the elements in A that are less than 9 AND even numbered.
Explanation / Answer
%matlab code
A = [5, 8, 12, 15, 13; 11, 15, 4, 9, 4; 10, 6, 8, 3, 13; 3, 9, 11, 3, 4; 2, 4, 14, 4, 14]
%part (a)
[row,column] = find(A < 9);
disp('elements in A less than 9: ');
for i=1:length(row)
fprintf('%d ', A(row(i),column(i)));
end
fprintf(' ');
%part (b)
[row,column] = find(A < 9 & A > 2);
disp('elements that are less than 9 AND greater than 2: ');
for i=1:length(row)
fprintf('%d ', A(row(i),column(i)));
end
fprintf(' ');
%part (c)
[row,column] = find(A < 9 & mod(A,2) == 0);
disp('elements in A that are less than 9 AND even numbered: ');
for i=1:length(row)
fprintf('%d ', A(row(i),column(i)));
end
fprintf(' ');
%{
output:
A =
5 8 12 15 13
11 15 4 9 4
10 6 8 3 13
3 9 11 3 4
2 4 14 4 14
elements in A less than 9:
5 3 2 8 6 4 4 8 3 3 4 4 4
elements that are less than 9 AND greater than 2:
5 3 8 6 4 4 8 3 3 4 4 4
elements in A that are less than 9 AND even numbered:
2 8 6 4 4 8 4 4 4
%}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.