在MATLAB中从矩阵数组创建对角矩阵 [英] Creating diagonal matrix from array of matrices in MATLAB
问题描述
我对如何从矩阵数组中创建对角矩阵感兴趣. 我在MATLAB中创建了一个矩阵数组:
I am interested how to create a diagonal matrix from an array of matrices. I created an array of matrices in MATLAB:
X<62x62x1000>
它由1000个矩阵组成,尺寸为62x62
X<62x62x1000>
it consists of 1000 matrices with dimensions 62x62
我想创建一个尺寸为62000x62000的矩阵,其中包含数组X沿主对角线的1000个子矩阵.
I want to create a matrix of dimensions 62000x62000 with 1000 sub matrices from array X along main diagonal.
除了M=blkdiag(X(:,:,1), X(:,:,2), X(:,:,3)...)
之外,您是否有其他任何用法的线索,因为这可能要花很多篇幅.
Do you have any clue how to do this, except M=blkdiag(X(:,:,1), X(:,:,2), X(:,:,3)...)
because that would be to much writing.
推荐答案
可能的解决方案
M = kron(speye(1000),ones(62));
M(logical(M)) = X(:);
使用kron
创建一个62000 * 62000稀疏矩阵M
,该稀疏矩阵的对角线包含1000个1的块,然后将其替换为X
的元素.
With kron
a 62000*62000 sparse matrix M
is created that contains 1000 blocks of ones on its diagonal, then replace ones with elements of X
.
这篇关于在MATLAB中从矩阵数组创建对角矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!