简体   繁体   中英

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

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM