Help with finding the min/max for 3 matrices

1 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2016-4-14
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"

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by