Matlab, find index of minimum value with the condition that it must be negative

arrays, find, matlab, min

Solution

Suppose the input matrix is `A`, this should do the trick:

find(A==min(A) & A<0)

For example:

>> A = [1, 2, 3, 4];
>> B = [1, 4, -7, -2];
>> find(A==min(A) & A<0)

ans =

   Empty matrix: 1-by-0

>> find(B==min(B) & B<0)

ans =

     3

Problem

In a given array, I need to find the index of the minimum value in an array, but only if it is negative. For example : `[1, 2, 3, 4]` would return no indices and `[1, 4, -7, -2]` would return `3` I was thinking that it must be simple with the `find()` command, but I couldn't figure out how to use it for this specific situation.

Original source