Help with finding the min/max for 3 matrices
2 ビュー (過去 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.
0 件のコメント
採用された回答
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!