Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MATLAB中找到多维矩阵的最大值或最小值?_Matlab_Multidimensional Array_Indexing_Max_Min - Fatal编程技术网

如何在MATLAB中找到多维矩阵的最大值或最小值?

如何在MATLAB中找到多维矩阵的最大值或最小值?,matlab,multidimensional-array,indexing,max,min,Matlab,Multidimensional Array,Indexing,Max,Min,我在MATLAB中有一个4D测量阵列。每个尺寸表示测量的不同参数。我想找到每一个的最大值和最小值以及索引(即哪个参数) 最好的方法是什么?我想我可以在每个维度上取max of the max of the max,但这似乎是个难题。快速示例: %# random 4 d array with different size in each dim A = rand([3,3,3,5]); %# finds the max of A and its position, when A is view

我在MATLAB中有一个4D测量阵列。每个尺寸表示测量的不同参数。我想找到每一个的最大值和最小值以及索引(即哪个参数)

最好的方法是什么?我想我可以在每个维度上取max of the max of the max,但这似乎是个难题。

快速示例:

%# random 4 d array with different size in each dim
A = rand([3,3,3,5]);

%# finds the max of A and its position, when A is viewed as a 1D array
[max_val, position] = max(A(:)); 

%#transform the index in the 1D view to 4 indices, given the size of A
[i,j,k,l] = ind2sub(size(A),position);
查找最小值作为练习:)

评论如下: 如果您不知道数组A的维数,因此无法写入“
[i,j,k,l]=
”部分,请使用以下技巧:

indices = cell(1,length(size(A)));

[indices{:}] = ind2sub(size(A),position);

对于二维数组,假设I 您可以只使用最小/最大功能两次。 n维数组的n次。 例如:
a=[2 3 4;5 6 7;-2 7 87;911 7 34]

for minimum:  min(min(a,[],1))
             ->  the answer will be -2. 
也可以将“最小/最大”中的尺寸参数设置为2。由于这是两次调用函数,因此第二次调用维度u选择的最小/最大元素向量


类似地,您可以使用
(max(max(max(a,[]),1))
找到最大值。

这是Matlab的方法。如果您想找到绝对最大值,请使用
max(abs(a(:)
并与
符号(a(位置))相乘
以防你对符号感兴趣。这是一个很好的解决方案,它在Matlab中使用,没有任何循环。如果你不知道a的维数,你能得到一个最大位置索引作为向量吗?例如,在函数中。@Adrien:请看常见问题解答,看看如何格式化你的帖子。解决最后一条评论的一个方便函数是IND2SUBV由Tom Minka编写,作为其Lightspeed工具箱的一部分:,您可以在此处找到函数:虽然在标记的dupe中回答了明确的问题,但此处的所有答案都回答了标题中的问题。这是两个非常不同的场景,我不同意重复标记。