You are given a 4x6 matrices, A and B in your workspace. Create a script file in
ID: 3761443 • Letter: Y
Question
You are given a 4x6 matrices, A and B in your workspace. Create a script file in(MATLAB) and include the command: load HW8 in your script. Use nested for loops to accomplish the following: Create a matrix C that has entries with the maximum of A and B (i.e., if A(r,c) exceeds B(r,c) then C(r,c) equals A(r,c) otherwise C(r,c) = B(r,c)). Display matrix C after the for loops. Count how many entries in Matrix A are greater than the corresponding entries in B. Display the count after the for loops. Count how many entries in Matrix B are greater than the corresponding entries in A. Display the count after the for loops. Count how many entries in Matrix A are equal to the corresponding entries in B. Display the count after the for loops. (Note: it is certainly possible to do this problem without loops, but the purpose of this problem is to practice nested loops so use them in your script) Run your code and paste your script and the resulting output in the spaces indicated below.
Output to Command Window:
Script:
Explanation / Answer
Script:
load hw8
A=[1,2,3,4,5,6;7,8,9,10,11,12;13,14,15,16,17,18;19,20,21,22,23,24]
B=[1,12,31,43,5,36;37,18,79,170,181,120;13,104,15,106,7,18;1,2,21,22,23,24]
equal_ele = 0
a_greater = 0
b_greater = 0
%creating C matrix
for i = 1:4
for j = 1:6
if A(i,j) > B(i,j)
C(i,j) = A(i,j);
else
C(i,j) = B(i,j);
end
end
end
%displaying C matrix
fprintf("Matrix C: ")
for i = 1:4
for j = 1:6
fprintf("%d ", C(i,j))
end
fprintf(" ")
end
% calculating greater element in A matrix
for i = 1:4
for j = 1:6
if A(i,j) > B(i,j)
a_greater += 1;
end
end
end
%displaying greter element in A matrix
fprintf("Number OfGreater Element in Matrix A than in Matrix B = %d ",a_greater)
% calculating greater element in B matrix
for i = 1:4
for j = 1:6
if A(i,j) < B(i,j)
b_greater += 1;
end
end
end
%displaying greter element in B matrix
fprintf("Number Of Greater Element in Matrix B than in Matrix A = %d ",b_greater)
% calculating equal element in A,B matrix
for i = 1:4
for j = 1:6
if A(i,j) == B(i,j)
equal_ele += 1;
end
end
end
%displaying equal element in A,B matrix
fprintf("Number Of Equal Elements in Matrix A and Matrix B= %d ",equal_ele)
Output to Command Window:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.