如何在MATLAB中求出每个三维阵列切片的最大值和位置?

时间:2021-03-25 21:34:36

What is the fastest way of calculating the maximum value, with it's corresponding index, of each 'slice' of a 3D array?

用它对应的索引计算3D数组的每个“片”的最大值的最快方法是什么?

2 个解决方案

#1


4  

Say you have A with n slices (here I just made each slice 10 by 10, but this can be changed to any size):

假设你有一个有n片的A(这里我只是把每片10×10,但是这个可以改变成任何大小):

A = rand(10,10,n);

You can reshape it to n-columns matrix, then take the maximum of each column:

你可以将它重塑为n列矩阵,然后取每列的最大值:

[val,ind] = max(reshape(A,[],n),[],1);

The first output val will be an n-element vector with all the maximum values, and the second output ind will be their row index in the reshaped A.

第一个输出的val将是一个具有所有最大值的n元素向量,第二个输出的ind将是它们在重构后的A中的行索引。

Then you get the size of the slices:

然后得到切片的大小:

sz = size(A);

and use it to find the row (r) and column (c) of each maximum element in each slice:

并利用它求出每个切片中每个最大元素的行(r)和列(c):

[r,c] = ind2sub(sz(1:2),ind)

So in this example (using rand and 10x10x6 array for A) you would get something like this at the end (but with different values):

在这个例子中(A使用rand和10x10x6数组)你最后会得到这样的结果(但是值不同):

val =
      0.99861      0.98895      0.98681      0.99991      0.96057      0.99176
r =
     9     7     3     8     2     9
c =
     1     1     8    10    10     5

#2


0  

If you have a matrix A with n layers, you can apply max function in two steps to get a 1 x 1 x n matrix with max of each layer

如果你有一个有n层的矩阵a,你可以用两个步骤应用max函数得到一个1×1×n的矩阵每个层的最大值

A = rand(10,10,n);
layer_max = max(max(A,[],1),[],2); % 1 x 1 x n matrix, use squeeze to remove extra dims 
layer_max = squeeze(layer_max);

#1


4  

Say you have A with n slices (here I just made each slice 10 by 10, but this can be changed to any size):

假设你有一个有n片的A(这里我只是把每片10×10,但是这个可以改变成任何大小):

A = rand(10,10,n);

You can reshape it to n-columns matrix, then take the maximum of each column:

你可以将它重塑为n列矩阵,然后取每列的最大值:

[val,ind] = max(reshape(A,[],n),[],1);

The first output val will be an n-element vector with all the maximum values, and the second output ind will be their row index in the reshaped A.

第一个输出的val将是一个具有所有最大值的n元素向量,第二个输出的ind将是它们在重构后的A中的行索引。

Then you get the size of the slices:

然后得到切片的大小:

sz = size(A);

and use it to find the row (r) and column (c) of each maximum element in each slice:

并利用它求出每个切片中每个最大元素的行(r)和列(c):

[r,c] = ind2sub(sz(1:2),ind)

So in this example (using rand and 10x10x6 array for A) you would get something like this at the end (but with different values):

在这个例子中(A使用rand和10x10x6数组)你最后会得到这样的结果(但是值不同):

val =
      0.99861      0.98895      0.98681      0.99991      0.96057      0.99176
r =
     9     7     3     8     2     9
c =
     1     1     8    10    10     5

#2


0  

If you have a matrix A with n layers, you can apply max function in two steps to get a 1 x 1 x n matrix with max of each layer

如果你有一个有n层的矩阵a,你可以用两个步骤应用max函数得到一个1×1×n的矩阵每个层的最大值

A = rand(10,10,n);
layer_max = max(max(A,[],1),[],2); % 1 x 1 x n matrix, use squeeze to remove extra dims 
layer_max = squeeze(layer_max);