Why wont it output index?
1 回表示 (過去 30 日間)
古いコメントを表示
Hunter Steele
2019 年 10 月 8 日
編集済み: Sulaymon Eshkabilov
2019 年 10 月 9 日
function[max,index]=MyMax(x)
max=0;
[n,m]=size(x);
for i=1:m
if max<x(i)
max=x(i);
index=i;
output = [max, index];
end
end
end
0 件のコメント
採用された回答
Sulaymon Eshkabilov
2019 年 10 月 9 日
Hi,
You have done everything correctly except for a couple of minor flaws. Here is the corrected code:
function output = MyMax(x)
MAX=0; % It is better to avoid MATLAB's function names, such as, max, min, etc to name variables
[n, m]=size(x);
for i=1:m
if MAX<x(i)
MAX=x(i);
index=i;
output = [MAX, index];
end
end
end
This can be tested:
>> x=linspace(-pi,pi);
>> OUT = MyMax(x)
OUT =
1.0e+02 *
0.031415926535898 1.000000000000000
Note that the easy solution of your exercise is this simple command:
[MAX_VAL, INDEX] = max(x)
Good luck.
その他の回答 (1 件)
Sulaymon Eshkabilov
2019 年 10 月 9 日
編集済み: Sulaymon Eshkabilov
2019 年 10 月 9 日
Just use:
[MAX_VAL, INDEX] = min(x) % instead of max()
Good luck.
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!