[英]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: 我尝试使用min
函数,但是遇到了这个错误:
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. 我认为您正在尝试使用min
的直接输出来访问数组中的条目。 The return value of min
is the actual minimum value not the index in the array. min
的返回值是实际最小值,而不是数组中的索引。
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
. 这将返回minval = -15
和minindex = 1
。 With minindex
you can address an entry in an array. 使用minindex
您可以寻址数组中的条目。 For example in the values
-array: 例如,在values
-array中:
values(minindex)
This returns of course -15
. 这将返回课程-15
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.