首页 文章

Matlab:逐行组合多个矩阵

提问于
浏览
0

我有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]

2 回答

  • 1

    你可以在这里下载函数 interleave2 https://au.mathworks.com/matlabcentral/fileexchange/45757-interleave-vectors-or-matrices

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

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

  • 1

    这是一个通用的解决方案,允许您将所需的多个矩阵(具有匹配的列数)放入起始cell array 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 是其行数 . 通过连接这些索引和它们,我们可以得到一个新的索引,可以用来对vertically-concatenated矩阵集的行进行排序,使它们交错 .

相关问题