2015-06-27 134 views
1

我有一个3x3x2000的旋转矩阵阵列,需要转换为2000x9的阵列。Matlab:将三维阵列重塑为二维阵列

我想我必须使用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

举一个输入**和**所需输出的小例子,它将帮助潜在的志愿者知道他们是否得到对的。提示:您可能不得不在转换中包含一些转置('。')操作,并且不要忘记_Matlab_是'主要列'(例如_linear索引按列而不是lines_工作)。 – Hoki

回答

1

如何简单for -loop?

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 -loops ...

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