代码之家  ›  专栏  ›  技术社区  ›  Nathan Fellman

如何在matlab中找到多维矩阵的最大值或最小值?[复制品]

  •  10
  • Nathan Fellman  · 技术社区  · 16 年前

    这个问题已经有了答案:

    我在matlab中有一个4d的测量数组。每个维度表示测量的不同参数。我想找出每个的最大值和最小值以及索引(即哪个参数)。

    最好的方法是什么?我想我可以取每个维度最大值的最大值,但这看起来像是一个拼凑。

    2 回复  |  直到 12 年前
        1
  •  34
  •   Jonas    16 年前

    快速实例:

    %# 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);
    
        2
  •  0
  •   Ria    12 年前

    对于二维数组,假设我 您只需使用min/max函数两次。 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(a,[],1)) 找出最大值。