Help with finding the min/max for 3 matrices

3 views (last 30 days)
I have a function mfile here
function[maxm]=my_matrix(a)
n=size(a);
maxm=-inf;
minm=inf;
for i=1:1:n(1);
for j=1:1:n(2);
if(a(i,j)>maxm);
maxm=a(i,j);
else(a(i,j)<minm);
minm=a(i,j);
end
end
end
and three matrices that display the following mins and maxs
a=[3 -2 1;4 0 5;1 2.2 -3] maxm=5
[m]=my_matrix(a); minm=-3
a=[4 2 1;9 3 5] maxm=9
[m]=my_matrix(a); minm=5
a=[9 8;7 6;5 4] maxm=9
[m]=my_matrix(a); minm=4
It shows the correct outputs for all three matrices except the minm for the second matrix,it should be 1, not 5. What do I need to change to get it to show the correct min? And yes, I know I could simply just use the min/max command, but for this assignment I'm not allowed to use commands like that.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Apr 2016
The first value in the array will be greater than -inf and less than inf but you do not update both of those variables, only the first, because you use "elseif"

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!