【问题标题】:Reshape to vertically tile blocks of columns below previous ones重塑为垂直平铺列块下方的列
【发布时间】:2015-01-11 02:12:58
【问题描述】:

在我的脚本中,我生成了一个矩阵,其中每一列至少与另一列耦合。例如,第 1 列与第 2 列耦合,第 3 列与第 4 列耦合,等等……但我也可以将第 3 列与第 3 列或第 4 列与第 4 列或任何其他数字耦合。

目前这只是一个图像,但我想将耦合的列移动到自己的行上,以便我可以使用 any() 或 sum() 轻松地将它们混合。

这个例子会更清楚:

A = reshape(1:12, 3, []) % A is the matrix I start with, this reshape is OK
A =

    1    4    7   10
    2    5    8   11
    3    6    9   12

reshape(A, [], 2) % this reshape is not OK
ans =

    1    7
    2    8
    3    9
    4   10
    5   11
    6   12

但是,我希望答案是:

ans =

    1    4
    2    5
    3    6
    7   10
    8   11
    9   12

正如我所说,此示例仅适用于 2 列,但在我的用例中,我还需要支持任意数量的列对。这里有 3 列:

B = reshape(1:18, 3, [])
B =

    1    4    7   10   13   16
    2    5    8   11   14   17
    3    6    9   12   15   18

reshape(B, [], 3)
ans =

    1    7   13
    2    8   14
    3    9   15
    4   10   16
    5   11   17
    6   12   18

我想要什么:

ans =

    1    4   7
    2    5   8
    3    6   9
   10   13  16
   11   14  17
   12   15  18

有没有办法以矢量化的方式做到这一点?

【问题讨论】:

  • This 可能会有所帮助

标签: matlab octave reshape tiling


【解决方案1】:

假设M 是输入矩阵,看看这是否适合你 -

ncols = 2; %// number of columns (needs to be edited)
[m,n] = size(M) %// get size of input matrix for later usage
r = numel(M)/(m*ncols);
out = reshape(permute(reshape(M,m,ncols,[]),[1 3 2]),m*r,[])

示例运行 -

M =
     1     4     7    10
     2     5     8    11
     3     6     9    12
ncols =
     2
out =
     1     4
     2     5
     3     6
     7    10
     8    11
     9    12

M =
     1     4     7    10    13    16
     2     5     8    11    14    17
     3     6     9    12    15    18
ncols =
     3
out =
     1     4     7
     2     5     8
     3     6     9
    10    13    16
    11    14    17
    12    15    18

涵盖另一个可能的预期问题

照你的话来说 - "column 1 is coupled with column 2, column 3 is coupled with column 4, etc... But I could also couple columns 3 by 3 or 4 by 4 or any other number",我感觉到你实际上可能正在寻找输入矩阵列的所有可能组合,并将它们垂直连接起来形成一个 slenderish 矩阵作为输出。解决方案的这一部分将涵盖该基础。实现这样一个目标的代码(如果这就是你希望的意思) 会是这样的 -

ncols = 2; %// number of columns (needs to be edited)
[m,n] = size(M) %// get size of input matrix for later usage
combs = dec2base(0:n^2-1,n,ncols)-'0'+1 %// find combinations
combsp = permute(combs,[3 2 1]) %// make a 3D array of those combinations

idx = bsxfun(@plus,[1:m]',(combsp-1)*m) %//'# Indices as a 3D array
idx1 = reshape(permute(idx,[1 3 2]),m*size(idx,3),[]) %// vertically concatenate 
                                            %// 3D indices array into a 2D array
out = M(idx1) %// desired output

一个示例运行 -

M =
     6     7     3     6
     3     1     6     3
     5     1     4     2
     
ncols = 2

out =
     6     6
     3     3
     5     5
     6     7
     3     1
     5     1
     6     3
     3     6
     5     4
     6     6
     3     3 ....    

【讨论】:

  • 很好的答案,第一种情况是我所追求的,但第二种情况也将非常有用,这解决了另一个我仍然需要解决的问题。但是,这个答案在使用稀疏矩阵的 Octave 中不起作用,因为 Octave 还不能处理 N-D 中的稀疏矩阵,只能处理 2D ......这是我的情况。该死的八度。
  • @user1121352 那么,稀疏的东西不适用于第一种情况或第二种情况?当我发布这些方法时,不要以为我的想法很稀疏,对此感到抱歉:)
  • 我用第一种情况进行了测试,但我想它也不适用于第二种情况。没关系,我没有在问题中提到稀疏,因为我不知道这个限制,这是特定于 Octave 的......我认为无论如何在 Octave 中做稀疏矩阵的垂直平铺没有任何好的方法,因此您的答案被接受(要么必须转换为达到稀疏目的的密集矩阵,要么使用单元格数组,但它也不会是稀疏的)。
  • 此外,根据文档,此 Octave 限制是临时的,希望将来会修复。
【解决方案2】:

Divakar 的解决方案是最好的,但如果像我一样使用稀疏矩阵和 Octave,Octave 还不支持 N-D 稀疏矩阵,所以你不能像 Divakar 那样重塑:

首先,虽然基本上可以有 N 维 稀疏对象,Octave 稀疏类在此不允许它们 时间;稀疏类的所有实例都必须是二维的。这 表示 SparseMatrix 实际上更类似于 Octave 的 Matrix 类比它的 NDArray 类。

来源:GNU Octave's documentation

唯一的解决方法是使用基于循环的解决方案,例如这个函数:

function B = vertical_tile(A, ncols)

B = [];
if issparse(A)
    B = sparse(B);
end

for i=1:ncols
    B = [B A(:, i:ncols:end)];
end
B = reshape(B, [], ncols);

end

结果:

vertical_tile(A, 2)
ans =

    1    4
    2    5
    3    6
    7   10
    8   11
    9   12

vertical_tile(B, 3)
ans =

    1    4    7
    2    5    8
    3    6    9
   10   13   16
   11   14   17
   12   15   18

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 2012-04-16
    • 2021-09-30
    • 2020-11-22
    • 1970-01-01
    • 2021-12-29
    • 2013-01-26
    相关资源
    最近更新 更多