Sparse Block diagonal matrix
古いコメントを表示
I have an N by M matrix and I want to store its columns in block diagonal form. This matrix is enormous, so if I can directly store it as a sparse matrix that would be helpful.
To give more detail:
X = ones(30, 1000);
And I want this without having to type it out (because the matrix actually has 1 million columns).
sparse(blkdiag(X(:,1), X(:,2), X(:,3), .... , X(:,1000)).
By the way, my best attempt is this:
a = cell(M,1);
for i = 1:M
[a{i}]= deal(sparse(X(:,i)));
end
X=blkdiag(a{:});
But I'm using a loop and am worried this is inefficient.
採用された回答
その他の回答 (2 件)
Alec
2014 年 7 月 18 日
Or, if you can, call `sparse` before `blkdiag`:
X = ones(30, 1000);
X = num2cell(sparse(X),1);
B = blkdiag(X{:});
Daniel Fortunato
2022 年 4 月 21 日
You can also use MATLAB's internal sparse block-diagonal routine, which will create a sparse block-diagonal matrix even from dense blocks:
B = matlab.internal.math.blkdiag(X{:});
カテゴリ
ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!