MATLAB code. Please be commented and clean. I DO give thumbs up for correct work
ID: 3705764 • Letter: M
Question
MATLAB code. Please be commented and clean. I DO give thumbs up for correct work. Thank you!
Write a general code (scr3) to generate generally n x m positive integer matrix with no elements larger than 11. Use two functions fun31, and fun32 as shown bellow The code is supposed to find fun31 fun31 fun31 fun31 fun32 fun32 The percentage of odd elements. The percentage of even numbers. The percentage of the elements that divided by 3 The percentage of the elements that less than or equal 3. How many elements are equal to 11. . . Which row has the maximum summation of its elements Make sure that the code presents the result through the script file (scr3) in a professional way.Explanation / Answer
-----------------------fun31.m----------------------
function fun31(arr)
% get the dimensionsof matrix
[ n , m ] = size(arr);
% store the total no of elements
total = n * m;
odd = 0;
even = 0;
div_by_3 = 0;
less_than_equal_to_3 = 0;
% traverse through rows
for i = 1 : n
% traverse through columns
for j = 1 : m
% if current element is even
if mod( arr(i,j) , 2 ) == 0
even = even + 1;
% if current element is odd
else
odd = odd + 1;
end
% if curent element is divisible by 3
if mod( arr(i,j) , 3 ) == 0
div_by_3 = div_by_3 + 1;
end
% if current element is less than equal to 3
if arr(i , j) <= 3
less_than_equal_to_3 = less_than_equal_to_3 + 1;
end
end
end
fprintf('Percentage of Odd elements : %f ' , odd / total * 100 );
fprintf('Percentage of Even elements : %f ' , even / total * 100 );
fprintf('Percentage of elements divisible by 3 : %f ' , div_by_3 / total * 100 );
fprintf('Percentage of elements less than or equal to 3 : %f ' , less_than_equal_to_3 / total * 100 );
end
-------------------------fun32.m-------------------------
function fun32(arr)
% get the dimensionsof matrix
[ n , m ] = size(arr);
equal_to_11 = 0;
% traverse through rows
for i = 1 : n
% traverse through columns
for j = 1 : m
% if current element is equal to 11
if arr(i,j) == 11
equal_to_11 = equal_to_11 + 1;
end
end
end
% store the row with maximum summation
row = -1;
summation = -1;
% traverse through rows
for i = 1 : n
% store the sum of current row
temp = 0;
% traverse through columns
for j = 1 : m
temp = temp + arr(i , j);
end
% if the current row has sum greater than summation
if temp > summation
row = i;
end
end
fprintf('No of elements equal to 11 : %d ' , equal_to_11 );
fprintf('Row with maximum summation : %d ' , row );
end
----------------------------main.m----------------------------
prompt = 'Enter n : ';
n = input(prompt);
fprintf(' ');
prompt = 'Enter m : ';
m = input(prompt);
% generate a random nxm matrix with element not exceeding 11
arr = randi( 11 , n , m );
fun31(arr);
fun32(arr);
Sample Output
Enter n : 4
Enter m : 5
Percentage of Odd elements : 65.000000
Percentage of Even elements : 35.000000
Percentage of elements divisible by 3 : 30.000000
Percentage of elements less than or equal to 3 : 35.000000
No of elements equal to 11 : 3
Row with maximum summation : 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.