Reputation: 9
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?
Upvotes: 0
Views: 2596
Reputation: 13923
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
.
Upvotes: 1