如何在MATLAB中找到数组中的最大值及其索引?

问题描述:

假设我有一个数组,a = [2 5 4 7].返回最大值及其索引的函数是什么?

Suppose I have an array, a = [2 5 4 7]. What is the function returning the maximum value and its index?

例如,在我的例子中,该函数应该返回 7 作为最大值和 4 作为索引.

For example, in my case that function should return 7 as the maximum value and 4 as the index.

函数为max.要获得第一个最大值,您应该这样做

The function is max. To obtain the first maximum value you should do

[val, idx] = max(a);

val 是最大值,idx 是它的索引.

val is the maximum value and idx is its index.