The code you provided is:
A(~A) = NaN; minNonZero = min(A);
Is the following done:
- Create logical index
- Apply logical index to
A - Change A by Assigning
NaN Values - Get the minimum of all values, not including
NaN values
Please note that this leaves you with a modified A , which may be undesirable. But more importantly, it has some drawbacks, because you spend time changing A and, possibly, even getting a minimal large matrix. Therefore, you can speed things up (and even reduce one line) by doing the following:
minNonZero = min(A(logical(A)))
Basically, you now skipped step 3 and possibly reduced step 4.
In addition, you seem to get additional slight acceleration:
minNonZero = min(A(A~=0))
I have no good reason for this, but it seems that step 1 is now done more efficiently.
source share