In a script file (sc2), write a general code to generate two m x n positive inte
ID: 3720352 • Letter: I
Question
In a script file (sc2), write a general code to generate two m x n positive integer matrices with no elements larger than 10, matrix A and matrix B. Then the code will merge these two matrices to compute matrix C [A B]. Use a function file (fu2) that has three inputs (A,B,C) and five scaler outputs (P1,P2,P3,P4,P5) that will go back to the script file to be printed on the screen in a professional way: 2. P1: the summation of odd numbers in matrix C P2: the summation of even numbers in matrix C P3: the number of elements that equal 10 in matrix O P4: which row of matrix C that has the maximum summation of its elements P5: How many elements in matrix A that equals to their corresponding elements in matrix B Run the code for m n 20 and write down the results. P3 P4Explanation / Answer
sc2.m CODE:
m=20;
n=20;
A=randi(10,m,n); %- this function creates randon matrix of max integer element 10 of matrix size mxn
B=randi(10,m,n);
C=[A B];
fu2 function code:
function [p1,p2,p3,p4,p5]=fu2(A,B,C)
[m,n]=size(A); %-m is row and n is column
[m1,n1]=size(C); %- m1 is row and n1 is column
%----Summation of ODD number in matrix C---%
p1=0; %- p1 will contain sum of odd numbers
for i=1:m1
for j=1:n1
if(mod(C(i,j),2)==1)
p1=p1+C(i,j);
end
end
end
%----Summation of EVEN number in matrix C---%
p2=0; %- p2 will contain sum of even numbers
for i=1:m1
for j=1:n1
if(mod(C(i,j),2)==0)
p2=p2+C(i,j);
end
end
end
%----Number of elements that equal 10 in matrix C---%
p3=0; %- p3 will contain number of element
for i=1:m1
for j=1:n1
if(C(i,j)==10)
p3=p3+1;
end
end
end
%----Which row contains maximum summation of its elements---%
max_val=0;
p4=0; %- p4 will contain row number
for i=1:m1
val=sum(C(i,:));
if(val>=max_val)
max_val=val;
p4=i;
end
end
%----how many elements in matrix A that equals to theirs corresponding element is ,matrix B---%
p5=0; %- p5 will contain number of element
for i=1:m
for j=1:n
if(A(i,j)==B(i,j))
p5=p5+1;
end
end
end
end
DATA:
INPUT:
A Matrix :
3 2 9 7 2 4 4 3 4 7 1 8 2 6 1 4 3 10 1 4
5 4 10 7 3 8 7 8 2 8 8 4 5 9 7 1 3 9 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 8 3 4 2 7 7 7 9 10 5 6 1 8 1 2 1 7 7 2 4 5 2 6 8 3 7 3 1 6 9 5 9
OUTPUT:
p1=1964
p2=2334
p3=66
p4=10
p5=39
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.