Matlab:将三维数组重塑为二维数组

时间:2015-06-27 16:38:23

标签: arrays matlab reshape

我有一个3x3x2000的旋转矩阵数组,我需要将其转换为2000x9数组。

我想我必须使用permute()和reshape()的组合,但我没有得到正确的输出顺序。

这就是我需要的:

First row of 3x3 array needs to be columns 1:3 in the output
Second row of 3x3 array needs to be columns 4:6 in the output
Third row of 3x3 array needs to be columns 7:9 in the output

我已尝试在以下代码中使用数字1 2 3的所有可能组合:

out1 = permute(input, [2 3 1]);
out2 = reshape(out1, [2000 9]);

但我总是以错误的顺序结束。有关Matlab新手的任何提示吗?

2 个答案:

答案 0 :(得分:1)

您的permute

混淆了
a = reshape(1:9*6, 3, 3, []); 

a是一个3x3x6矩阵,每个

a(:,:,i) = 9*(i-1) + [1 4 7
                      2 5 8
                      3 6 9];

所以

out1 = permute(a, [3,1,2]);
out2 = reshape(out1, [], 9);

或在一行

out3 = reshape(permute(a, [3,1,2]), [], 9);

所以

out2 = out3 =

     1     2     3     4     5     6     7     8     9
    10    11    12    13    14    15    16    17    18
    19    20    21    22    23    24    25    26    27
    28    29    30    31    32    33    34    35    36
    37    38    39    40    41    42    43    44    45
    46    47    48    49    50    51    52    53    54

答案 1 :(得分:1)

一个简单的for循环怎么样?

for i=1:size(myinput,3)
    myoutput(i,:)=[myinput(1,:,i) myinput(2,:,i) myinput(3,:,i)];
    % or
    % myoutput(i,:)=reshape(myinput(:,:,i),[],9);
end

使用permutereshape并不简单,但它透明且易于调试。一旦程序中的所有内容都运行完美,您就可以考虑在代码中重写这样的for - 循环......

相关问题