MATLAB: Find the non zero minimum and return the index

indexnon-zero

I have a matrix
x = [12 10 0 6]
I need to find the non zero minimum and return the index of this non zero minimum, I have wrote
[A,I]=min(x(x>0))
It return that
A=6; I=3;
How can I get
A=6; I=4
? Thank you.

Best Answer

If you want to temporarily change ‘x’, this works:
x = [12 10 0 6];
x(x == 0 ) = NaN;
[A,I] = min(x)
A =
6
I =
4
You can then change the NaN back to a zero if you want to:
x(isnan(x)) = 0
x =
12 10 0 6