Reshape matrix dimensions to a larger and sparse one
2 views (last 30 days)
Show older comments
Hi all,
I have matrix A with dimensions . I have also a sparse matrix B with dimensions with different elements.
I'd like to change the dimesions of A so it matches matrix B with the same sparsity/structure, i.e., the first two row elements in the first column of A to be put in a new column, and then the following two row elements to be put in another column, etc. Here are example matrices for help:
%matrix A with dimensions 10x4
A = [0.8147 0.1576 0.6557 0.7060;
0.9058 0.9706 0.0357 0.0318;
0.1270 0.9572 0.8491 0.2769;
0.9134 0.4854 0.9340 0.0462;
0.6324 0.8003 0.6787 0.0971;
0.0975 0.1419 0.7577 0.8235;
0.2785 0.4218 0.7431 0.6948;
0.5469 0.9157 0.3922 0.3171;
0.9575 0.7922 0.6555 0.9502;
0.9649 0.9595 0.1712 0.0344];
%matrix B with dimensions 40x20
B = [0.4387 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0.3816 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0.7655 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0.7952 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0.1869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0.4898 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0.4456 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0.6463 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0 0.7094 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0 0.7547 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0 0 0.2760 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0 0 0.6797 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0 0 0 0.6551 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0 0 0 0.1626 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 0 0 0.3500 0 0 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1966 0 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2511 0 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.6160 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.4733 ];
Any help would be appreicted.
1 Comment
James Tursa
on 7 Aug 2023
Is it possible for some of the "sparse pattern" elements in B to be 0? Some algorithms that one might use will break down if this is possible.
Accepted Answer
Bruno Luong
on 7 Aug 2023
A = [0.8147 0.1576 0.6557 0.7060;
0.9058 0.9706 0.0357 0.0318;
0.1270 0.9572 0.8491 0.2769;
0.9134 0.4854 0.9340 0.0462;
0.6324 0.8003 0.6787 0.0971;
0.0975 0.1419 0.7577 0.8235;
0.2785 0.4218 0.7431 0.6948;
0.5469 0.9157 0.3922 0.3171;
0.9575 0.7922 0.6555 0.9502;
0.9649 0.9595 0.1712 0.0344];
%matrix B with dimensions 40x20
B = [0.4387 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0.3816 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0.7655 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0.7952 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0.1869 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0.4898 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0.4456 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0.6463 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0 0.7094 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0 0.7547 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
0 0 0 0 0 0.2760 0 0 0 0 0 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0 0 0 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2435 0 0 0 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.9293 0 0 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.3500 0 0 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.1966 0 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.2511 0 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.6160 ;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.4733 ];
[i,j,b] = find(B);
AS=sparse(i,j,A(:));
full(AS)
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!