How do I rearrange a matrix in a loop

I want to change the order of each element in a matrix.
For example I need to transform matrix A into matrix B using a For loop.
A =
10 5
4 10
2 3
B =
10 5 0 0 0 0
0 0 4 10 0 0
0 0 0 0 2 3
10 0 4 0 2 0
0 5 0 10 0 3

 Accepted Answer

No loops needed:
a = num2cell(A,2);
B = blkdiag(a{:})

More Answers (0)

Categories

Find more on Mathematics 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!