How to find the min value of a sequence of negative numbers?

2.5k views Asked by At

I have a vector of negative values like this:

values = [-15,  -6.45,  -3.75, -5.55, -2.40]

i want to know which of these is the smallest and the corresponding index. I try using min function but i ran into this error:

Subscript indices must either be real positive integers or logicals.

How can i solve this annoying problem?

1

There are 1 answers

1
Matt On

I think you are trying to use the direct output of min to access an entry in an array. The return value of min is the actual minimum value not the index in the array.

To get the value and the index try using the following code:

values = [-15,  -6.45,  -3.75, -5.55, -2.40];
[minval,minindex] = min(values)

This will return minval = -15 and minindex = 1. With minindex you can address an entry in an array. For example in the values-array:

values(minindex)

This returns of course -15.