Help with finding the min/max for 3 matrices

4 visualizaciones (últimos 30 días)
Vinny
Vinny el 14 de Abr. de 2016
Respondida: Walter Roberson el 14 de Abr. de 2016
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Abr. de 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"

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by