Minimum value with row index
2 次查看(过去 30 天)
显示 更早的评论
I wrote the following code to get a minimum positive value and the row index as well but the output is giving wrong row index. Anyone can help me?
The answer should be...minba=0.005 ri=9
clc; clear all;
ba=[52 15 52 44 44 0 -25 -4 .05]; [minba ri]=min(ba((ba>0)))
0 个评论
采纳的回答
Sean de Wolski
2011-3-29
idx = min(find(ba==minba))
2 个评论
Matt Fig
2011-3-29
I understand the question to be "How do I find the minba and ri," not, "Given, minba, how to find the ri?"
更多回答(2 个)
Matt Fig
2011-3-29
If minba is not known before hand (the general case, and what it sounds like you want to have), then:
ba = [52 15 52 44 44 0 -25 -4 .05]; % Your data...
ba(ba<=0) = inf; % Take non-positive values out of the picture.
[minba,ri] = min(ba) % Find the min and index.
minba =
0.05
ri =
9
A faster alternative to the above, if the number of non-positives is a large fraction of the whole:
minba = min(ba((ba>0)));
ri = find(ba==minba,1);
3 个评论
Matt Fig
2011-3-29
How would you use FIND if minba wasn't known before hand? I think anything that could do this using FIND would be slower than what I showed...
Matt Fig
2011-3-29
But I was wrong. This is faster indeed, when the number of non-positives is a large fraction of the whole:
minba = min(ba((ba>0)));
ri = find(ba==minba,1);
Walter Roberson
2011-3-29
idx = find(ba > 0,1);
minba = ba(idx);
5 个评论
Matt Fig
2011-3-29
@Walter
Qouting the OP (without quotes of course)
I wrote the following code to ...
The answer _should_ _be_...minba=0.005 ri=9
So I assume Mohammad wanted to be shown the _correct_ way to get both minba and ri.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!