How to make diagonal matrices from a single column

7 visualizzazioni (ultimi 30 giorni)
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,

Risposta accettata

the cyclist
the cyclist il 14 Ott 2019
B = zeros(8,2);
B(1:2:end,1) = A(1:2:end);
B(2:2:end,2) = A(2:2:end);
  2 Commenti
Ed Eun
Ed Eun il 14 Ott 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 il 15 Ott 2019
Modificato: the cyclist il 15 Ott 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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by