从3D矩阵变量切片时将2D矩阵展平为1D的最佳方法

时间:2022-08-22 23:32:52

I have a 3D matrix in Matlab to store a sequence of 2D arrays. I'm having to find the maximal value and its row and column indices, which is pretty straightforward for a single variable that holds a 2D array as in

我在Matlab中有一个3D矩阵来存储一系列2D数组。我必须找到最大值及其行和列索引,这对于保存2D数组的单个变量非常简单。

A = rand(10,10);
[m,i] = max(A(:));
[I,J] = ind2sub( size(A) , i )

The trouble is that I cannot use this syntax for the 3D matrix

麻烦的是我不能将这种语法用于3D矩阵

A = rand(10,10,3);
[m,i] = max( A(:,:,1)(:) );
[I,J] = ind2sub(size( A(:,:,1) ), i )

Error: ()-indexing must appear last in an index expression.

I could create a temporary variable to store the 2D slice, but I'd thought I'd see if there's a better means of doing this, maybe making a call to reshape? Is there any way to use the simple linearizing/flattening operator (:) in this context?

我可以创建一个临时变量来存储2D切片,但我想我会看到是否有更好的方法可以做到这一点,也许会调用重塑?有没有办法在这种情况下使用简单的线性化/展平运算符(:)?

3 个解决方案

#1


4  

Here's what I'd do:

这是我要做的:

[B i]=max(reshape(A,[],size(A,3)));
[II,JJ]=ind2sub(size(A),i );

The only limitation is that it wont treat well cases where there is more than one max per 2D slice.

唯一的限制是它不会处理每个2D切片有多个最大值的情况。

#2


1  

You could convert it to a cell array and use cellfun

您可以将其转换为单元格数组并使用cellfun

B=mat2cell(reshape(A,[1, size(A,2).^2, size(A,3)]),[1],[size(A,2).^2], [ones(size(A,3),1)]);
[M,I]= cellfun(@max,B)
[R,C] = ind2sub(size(A),I);

M contains the maximum value and I the corresponding index.

M包含最大值,I包含相应的索引。


Assuming that A is a 3x3x2 array.

假设A是3x3x2阵列。

A =[

    0.7952    0.4456    0.7547
    0.1869    0.6463    0.2760
    0.4898    0.7094    0.6797];

A(:,:,2) =[

    0.6551    0.4984    0.5853
    0.1626    0.9597    0.2238
    0.1190    0.3404    0.7513];

Convert each slice into a 1x9x2 cell array

将每个切片转换为1x9x2单元阵列

B=mat2cell(reshape(A,[1, size(A,2).^2, size(A,3)]),[1],[size(A,2).^2], [ones(size(A,3),1)]);

B(:,:,1) = 

    [1x9 double]


B(:,:,2) = 

    [1x9 double]

Take the maximum of each slice. R is the row and C is the column for the respective maximum value in M.

取每片的最大值。 R是行,C是M中各自最大值的列。

[M,I]= cellfun(@max,B)
[R,C] = ind2sub(size(A),I)

R(:,:,1) =

     1

R(:,:,2) =

     2

C(:,:,1) =

     1

C(:,:,2) =

     2

#3


0  

Successively taking max will directly pull out the indices in (reverse) order:

连续取max将直接以(反向)顺序拉出索引:

A = rand(10,10,3);
[m,J] = max(max(A(:,:,1)));
[m,I] = max(A(:,J,1));

%check: A(I,J,1) == m

%check:A(I,J,1)== m

#1


4  

Here's what I'd do:

这是我要做的:

[B i]=max(reshape(A,[],size(A,3)));
[II,JJ]=ind2sub(size(A),i );

The only limitation is that it wont treat well cases where there is more than one max per 2D slice.

唯一的限制是它不会处理每个2D切片有多个最大值的情况。

#2


1  

You could convert it to a cell array and use cellfun

您可以将其转换为单元格数组并使用cellfun

B=mat2cell(reshape(A,[1, size(A,2).^2, size(A,3)]),[1],[size(A,2).^2], [ones(size(A,3),1)]);
[M,I]= cellfun(@max,B)
[R,C] = ind2sub(size(A),I);

M contains the maximum value and I the corresponding index.

M包含最大值,I包含相应的索引。


Assuming that A is a 3x3x2 array.

假设A是3x3x2阵列。

A =[

    0.7952    0.4456    0.7547
    0.1869    0.6463    0.2760
    0.4898    0.7094    0.6797];

A(:,:,2) =[

    0.6551    0.4984    0.5853
    0.1626    0.9597    0.2238
    0.1190    0.3404    0.7513];

Convert each slice into a 1x9x2 cell array

将每个切片转换为1x9x2单元阵列

B=mat2cell(reshape(A,[1, size(A,2).^2, size(A,3)]),[1],[size(A,2).^2], [ones(size(A,3),1)]);

B(:,:,1) = 

    [1x9 double]


B(:,:,2) = 

    [1x9 double]

Take the maximum of each slice. R is the row and C is the column for the respective maximum value in M.

取每片的最大值。 R是行,C是M中各自最大值的列。

[M,I]= cellfun(@max,B)
[R,C] = ind2sub(size(A),I)

R(:,:,1) =

     1

R(:,:,2) =

     2

C(:,:,1) =

     1

C(:,:,2) =

     2

#3


0  

Successively taking max will directly pull out the indices in (reverse) order:

连续取max将直接以(反向)顺序拉出索引:

A = rand(10,10,3);
[m,J] = max(max(A(:,:,1)));
[m,I] = max(A(:,J,1));

%check: A(I,J,1) == m

%check:A(I,J,1)== m