How to make diagonal matrices from a single column

8 次查看(过去 30 天)
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,

采纳的回答

the cyclist
the cyclist 2019-10-14
B = zeros(8,2);
B(1:2:end,1) = A(1:2:end);
B(2:2:end,2) = A(2:2:end);
  2 个评论
Ed Eun
Ed Eun 2019-10-14
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 2019-10-15
编辑:the cyclist 2019-10-15
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by