extracting the indices of minimum value in a vector, ignoring last value

I need to find the indices of the minimum positive value in a vector, ignoring the last value in the vector. if there is a tie, I need the first value based on indexes.
for example: if R = [120; 70; 50; 0] I want the incides for 50 back (3,1)
if R = [10; 16 ; 10; 30; 10; 0] I want the indices for the first 10 back (1,1)
thanks!!!!

답변 (1개)

Paulo Silva
Paulo Silva 2011년 3월 26일
a=R; %create a copy of R
%in order to only find positive numbers we do the next line
a(a<=0)=nan; %replace negative numbers and the zero with nan
[r,c]=find(a==min(a(1:end-1)),1) %find values
alternative and better:
[r,c]=find((R==min(a(1:end-1)))>0,1) %find values

댓글 수: 4

Thanks for your help!! question though,
this keeps returning 1,1 for me-
If R = [-10; 2 ; 4; 1; 10; 0]
I want (4, 1) back. How do I stipulate I am just concerned with the positive values?
i fixed the code, try it now
thank you, thank you!! you're awesome :)
@Brittany: Does this mean, that you accept the question? Then please press the corresponding button.

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

카테고리

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

질문:

2011년 3월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by