MATLAB:组合多个矩阵行

  • 本文关键字:组合 MATLAB matlab matrix
  • 更新时间 :
  • 英文 :


我有10个矩阵中的一些数据。每个矩阵都有不同数量的行,但是相同数量的列。

我想将所有10个矩阵结合到一个矩阵行,交错,这意味着该矩阵中的行看起来像:

row 1 from matrix 0
...
row 1 from matrix 9
row 2 from matrix 0
...
row 2 from matrix 9
...

示例(带3个矩阵):

Matrix 1: [1 2 3 ; 4 5 6; 7 8 9] Matrix 2: [3 2 1 ; 6 5 4] Matrix 3: [1 1 1 ; 2 2 2 ; 3 3 3] Combined matrix will be: [1 2 3 ; 3 2 1 ; 1 1 1 ; 4 5 6 ; 6 5 4 ; 2 2 2 ; 7 8 9 ; 3 3 3]

您可以在此处下载函数interleave2 https://au.mathworks.com/matlabcentral/fileexchange/45757-interleave-vectors-vectors-orm-matrices

z = interleave2(a,b,c,'row')

您可以看到该函数在源代码中的工作方式

这是一个通用解决方案,它允许您将想要的许多矩阵(带有匹配的列数)放入起始单元格数组Result

Result = {Matrix1, Matrix2, Matrix3};
index = cellfun(@(m) {1:size(m, 1)}, Result);
[~, index] = sort([index{:}]);
Result = vertcat(Result{:});
Result = Result(index, :);

这将为每个矩阵生成一个索引向量1:m,其中m是其行的数量。通过串联这些索引并对其进行排序,我们可以获得一个新索引,该索引可用于对垂直征收的矩阵的行进行排序,以使它们交织在一起。

最新更新