Minimum value with row index

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)))

 채택된 답변

Sean de Wolski
Sean de Wolski 2011년 3월 29일

0 개 추천

idx = min(find(ba==minba))

댓글 수: 2

Walter Roberson
Walter Roberson 2011년 3월 29일
idx = find(ba==minba,1);
Matt Fig
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
Matt Fig 2011년 3월 29일

2 개 추천

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

Sean de Wolski
Sean de Wolski 2011년 3월 29일
Out of curiosity, is it faster to overwrite with inf or to use find?
Matt Fig
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
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
Walter Roberson 2011년 3월 29일

0 개 추천

idx = find(ba > 0,1);
minba = ba(idx);

댓글 수: 5

Sean de Wolski
Sean de Wolski 2011년 3월 29일
This just finds the first value greater than zero; not necessaryily the min
minba = 52;
Your comment on my answer appears to be the best!
Matt Fig
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?"
Walter Roberson
Walter Roberson 2011년 3월 29일
Matt, we are working from the original code [minba ri]=min(ba((ba>0)))
Sean: good point. I'll have to rethink this.
Sean de Wolski
Sean de Wolski 2011년 3월 29일
Congrats Walter on your 1000th answer!
Matt Fig
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by