【问题标题】:How to reshape multiple dimensions with constraints如何使用约束重塑多个维度
【发布时间】:2018-09-19 00:45:15
【问题描述】:

我一直在尝试使用 reshape 来实现这种行为。我是 matlab 新手,所以我有点挣扎。

我有一个

3 x 2 x 5 矩阵称为 A

我想合并维度 1 到 5,不包括 4。在组合它们时,我想按顺序保留每行中的列。

我知道我可以做类似的事情

A = reshape(A, [12, 2, 1]) 但我担心这可能不会保留每行中列的顺序,并且不允许我排除维度。

例子:

(:,:,1) =

0.3168    3.1825
1.5841    1.5766
-0.7892    3.0073


(:,:,2) =

-0.2131    3.9708
2.9435    0.4964
-0.2131    2.5985

(:,:,3) =

0.5012    -3.5328
0.4551    2.1314
-1.5956    3.4161

(:,:,4) =

-0.12    -3.28
-0.51    -2.14
-1.56    -3.61


(:,:,5) =

6.5012    3.538
0.4551    0.314
-0.5956    0.411

输出,假设合并除第 4 个维度之外的所有维度

0.3168    3.1825
1.5841    1.5766
-0.7892    3.0073
-0.2131    3.9708
2.9435    0.4964
-0.2131    2.5985
0.5012    -3.5328
0.4551    2.1314
-1.5956    3.4161
6.5012    3.538
0.4551    0.314
-0.5956    0.411

【问题讨论】:

    标签: matlab


    【解决方案1】:

    您可以通过巧妙地使用reshape 来实现您想要的,正如您所提到的,以及索引。此外,您将需要使用permute(它会切换尺寸),因为您希望保持每个“切片”完好无损。

    首先,很高兴知道您可以使用索引排除第 4 个切片。考虑以下示例:

    A = randi(100,3,2,5)
    idxdim3 = [1,2,3,5];     % Create an array of integers corresponding to the slices you want
    B = A(:,:,idxdim3)       % Extract all the slices you want, discarding the 4th
    

    在这种情况下,这将为您留下一个大小为 [3 x 2 x 4] 的数组。接下来可以尝试使用reshape,但是会遇到问题。 reshape 向上工作,列是第一个维度,行是第二个维度,“切片”是第三个维度。正如您所说,您希望保持每行中的列按顺序排列,但reshape 会在它开始在第三维中工作之前更改这些列。所以我们需要先用permute做第二维和第三维的切换位置:

    % Leave 1 as the first dimension, but switches dimensions 2 and 3 around:
    C = permute(B,[1 3 2]);      
    

    现在我们可以reshape到合适的大小:

    D = reshape(C,[12 2 1]);
    

    这应该会给你你正在寻找的结果。当然,我们可以将这些语句中的大部分组合成一行,如下所示:

    A = randi(100,3,2,5);  % Put your data here instead
    idxdim3 = [1,2,3,5];
    B = reshape( permute( A(:,:,idxdim3), [1 3 2]), 12, 2);
    

    注意:如果您将来要使用不同大小的A,如果您指定其他维度,则可以让reshape 计算其中一个维度。以下也将起作用:

    B = reshape( permute( A(:,:,idxdim3), [1 3 2]), [], 2);
    

    reshape 将为您计算12

    示例输出:

    A(:,:,1) =
    44    80
    39    19
    77    49
    A(:,:,2) =
    45    76
    65    28
    71    68
    A(:,:,3) =
    66    50
    17    96
    12    35
    A(:,:,4) =
    59    26
    23    51
    76    70
    A(:,:,5) =
    90    14
    96    15
    55    26
    
    B =
    44    80
    39    19
    77    49
    45    76
    65    28
    71    68
    66    50
    17    96
    12    35
    90    14
    96    15
    55    26
    

    【讨论】:

      【解决方案2】:

      您需要使用permutereshape 的组合:

      % delete A[:,:,4]
      B = A(:,:,[1:3,5])
      % permute dimension 2 and 3, because you want to concatenate your array according to the 3nd dimension before the 2nd dimension.
      C = permute(B,[1 3 2])
      % Use reshape with the first parameter beeing '[]' so matlab will automatically compute the number of line needed, the number of column is fixed: it's 2 or size(B,2)
      C = reshape(C,[],size(B,2))
      

      【讨论】:

      • 不要粗鲁,但你为什么要发布与我的完全相同相同的信息和解决方案的答案,甚至是相同的变量名?你没有提供任何我没有提供的东西,你的答案是在我的一个小时后添加的。看起来你真的只是从我的答案中复制了代码行并将它们发布到你自己的地方。
      • @FlorisSA 嗨,我在您的回答后 6 分钟发布了这个答案,我没有注意到在我发布我的答案之前已经发布了答案。我没有删除我的答案有两个原因:1)我认为你的答案太长了,所以很难在不到 15 秒的时间内获得主要信息。 2)您在答案中固定了尺寸大小,使 sn-p 不可概括。
      猜你喜欢
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2018-02-21
      相关资源
      最近更新 更多