Azzera filtri
Azzera filtri

Help with finding the min/max for 3 matrices

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Walter Roberson
Walter Roberson il 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"

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by