MATLAB CODE. I do give thumbs up for correct answers Classwork#9 Using loops and
ID: 3725395 • Letter: M
Question
MATLAB CODE. I do give thumbs up for correct answers
Classwork#9 Using loops and if statement (if needed) write down a matlab code that first generate a random integer matrix A with the size (m,n), where m and n are two randomly generated integers that less than or equal 100. Then the code is supposed to count how many odd numbers and how many even numbers that matrix A has and print out the result professionally. (copy and paste your code to the word document) What is your code output for m-10, n =15 number of odd numbersnumber of even numbers m = 10, n =30 · number of odd numbers number of even numbers- s " , m=30, n-30 numbers.... number of even numbersExplanation / Answer
% randomly generate m and n in range 1 to 100
m = randi( 100 , 1 );
n = randi( 100 , 1 );
% generate a matrix of size mxn with random numbers in range 1 to 100
A = randi( 100 , m , n);
odd = 0;
even = 0;
for i = 1 : m
for j = 1 : n
% if curent element is even
if mod( A(i, j) , 2 ) == 0
even = even + 1;
% if curent element is odd
else
odd = odd + 1;
end
end
end
fprintf(' Number of Odd Elements : %d', odd);
fprintf(' Number of Even Elements : %d', even);
Sample Output
Number of Odd Elements : 1122
Number of Even Elements : 1188
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.