【问题标题】:Matlab: reshape 3-dimensional array into 2-dimensional arrayMatlab:将3维数组重塑为2维数组
【发布时间】:2015-06-27 16:38:22
【问题描述】:

我有一个 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 新手的任何提示?

【问题讨论】:

  • 给出一个输入期望输出的小例子,它将帮助潜在的志愿者知道他们是否做对了。提示:您可能必须在转换中包含一些转置 (.') 操作,并且不要忘记 Matlabcolumn major(例如 线性索引按列而不是按行工作)。

标签: arrays matlab reshape


【解决方案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...

【讨论】:

    【解决方案2】:

    你的 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
    

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 2014-04-21
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      相关资源
      最近更新 更多