Why is this not outputting anything?

function output = MyMin(x)
MIN_VALUE=0;
[m, n]=size(x);
for i=1:length(x)
if MIN_VALUE>x(i)
MIN_VALUE=x(i);
index=i;
output = [MIN_VALUE, index];
end
end

 採用された回答

meghannmarie
meghannmarie 2019 年 10 月 9 日
編集済み: meghannmarie 2019 年 10 月 9 日

0 投票

For this to work you need to set your intial MIN_VALUE to inf rather than 0. If its set to zero, it will only make output if you send it a negative number, otherwise it will never go into that if statement.
function output = MyMin(x)
MIN_VALUE=inf;
for i=1:length(x)
if MIN_VALUE>x(i)
MIN_VALUE=x(i);
index=i;
output = [MIN_VALUE, index];
end
end
end

2 件のコメント

Hunter Steele
Hunter Steele 2019 年 10 月 9 日
Thank you for making that clear!
Adam Danz
Adam Danz 2019 年 10 月 9 日
編集済み: Adam Danz 2019 年 10 月 9 日
I didn't see meghannmarie's answer, before posting mine (minutes apart). I just wanted to add that the min() function does what you're loop is doing and it's a lot more efficient.
[MIN_VALUE, index] = min(x);
The only difference is that the min() function splits the output into two while yours reutrns a 1x2 vector.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

2019 年 10 月 9 日

編集済み:

2019 年 10 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by