If by 2nd smallest you mean 2nd smallest distinct value:
A = [1 0 1 2 1 4 2 3];
uA = unique(nonzeros(A));
small2distinct = uA(2)
If by 2nd smallest you do mean 2nd smallest, so if the minimum is repeated twice, you do want the minimum:
A = [1 0 1 2 1 4 2 3];
sA = sort(nonzeros(A));
small2 = sA(2)
So, nonzeros to get all the non zero values, then sort or unique to sort the values, then take the 2nd one.
0 Comments
Sign in to comment.