将矩阵折叠成列 [英] Collapsing matrix into columns
问题描述
我有一个2D矩阵,其中列数始终是3的倍数(例如250×27
)-由于结果的重复组织(A,B,C
,A,B,C
,A,B,C
等) ).我希望重塑此矩阵,以创建一个具有3列的新矩阵-每列包含每种类型(A,B,C
)(例如2250×3
)的汇总数据.
I have a 2D matrix where the № of columns is always a multiple of 3 (e.g. 250×27
) - due to a repeating organisation of the results (A,B,C
, A,B,C
, A,B,C
, and so forth). I wish to reshape this matrix to create a new matrix with 3 columns - each containing the aggregated data for each type (A,B,C
) (e.g. 2250×3
).
因此在250×27
的矩阵中,列1,4,7,10,13,16,19,22,25
中的所有数据将合并以形成最终重整矩阵的第一列.
So in a matrix of 250×27
, all the data in columns 1,4,7,10,13,16,19,22,25
would be merged to form the first column of the resulting reshaped matrix.
结果整形矩阵中的第二列将包含列2,5,8,11,14,17,20,23,26
中的所有数据-依此类推.
The second column in the resulting reshaped matrix would contain all the data from columns 2,5,8,11,14,17,20,23,26
- and so forth.
在MATLAB中有一种简单的方法吗?我只知道如果要合并的列是相邻的(1,2,3,4,5,6
)而不是不相邻的(1,4,7,10,13,16
)等,如何使用reshape
.
Is there a simple way to do this in MATLAB? I only know how to use reshape
if the columns I wanted to merge were adjacent (1,2,3,4,5,6
) rather than non-adjacent (1,4,7,10,13,16
) etc.
推荐答案
从 @Divakar 的无耻偷窃:
B = reshape( permute( reshape(A,size(A,1),3,[]), [1,3,2]), [], 3 );
这篇关于将矩阵折叠成列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!