How to make diagonal matrices from a single column

8 views (last 30 days)
I want to make multiple diagonal matrices from a single colimn.
A=[1;2;3;4;5;6;7;8]
From this...
I want to make...
B=[1 0; 0 2]; [3 0; 0 4]; [5 0;0 6]; [7 0;0 8]
Then,
Each diagonal matrix is to be vertically attached.
So, from 10*1 to 10*2.
How can I do that?
Thanks,

Accepted Answer

the cyclist
the cyclist on 14 Oct 2019
B = zeros(8,2);
B(1:2:end,1) = A(1:2:end);
B(2:2:end,2) = A(2:2:end);
  2 Comments
Ed Eun
Ed Eun on 14 Oct 2019
Thanks and there is one more thing. if the dataset is small, this is good. What if the set has a single column with 3816 observation? Everty 72 obs are supposed to be a diagonal matrix. So, there are 52 sets of 72*72 diagonal matrices.
Is there an efficient way to do that? or, let me know some reference docs.
Thanks again.
the cyclist
the cyclist on 15 Oct 2019
Edited: the cyclist on 15 Oct 2019
I think you got some of your math wrong there, but something like this should work:
A = 1:3744;
B = zeros(52,72);
for row = 1:numel(A)
col = mod(ia-1,72) + 1;
B(row,col) = A(row);
end

Sign in to comment.

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices 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!