How to transfer a sparse matrix into a block diagnal matrix efficiently?
Mostrar comentarios más antiguos
Hi, Everyone:
Suppose I have a very large M*N sparse matrix A, where M=K*N, I need to equally split it into K N*N matrices and put all of them on the diagnal of a large matrix,i.e.:
[N*N(1) 0 0 0...0 0 0 ;
0 0 N*N(2) 0 0 ... 0 ;
0 0 . 0 0 ... 0 ;
0 0 0 0 . 0 0 ... 0 ;
0 0 0 0 0 . 0 ... 0 ;
0 0 ... 0 0 0 0 N*N(K)]'
I can't use loop because K is very big, so I tried to use:
B=mat2cell(A,N*ones(K,1),N);
C=[blkdiag(B{1:end,1})];
But I found mat2cell is still quite slow when K is very big, is there any other more efficient way to do this?
Many Thanks
Respuesta aceptada
Más respuestas (1)
1 comentario
Categorías
Más información sobre Risk Management Toolbox en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!