how to generate the matrix of (50,100) with its diagonal alone having values
2 次查看(过去 30 天)
显示 更早的评论
how to generate the matrix of (50,100) with its diagonal alone having values
0 个评论
采纳的回答
Stephen23
2018-1-7
Method one: indexing:
M = zeros(50,100);
M(1:51:50^2) = V % vector V must have 50 elements
A simple example:
>> M = zeros(5,10);
>> M(1:6:25) = 1:5
M =
1 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0
Method two: diag:
[diag(V),zeros(50)] % vector V has 50 elements
and an example:
>> [diag(1:5),zeros(5)]
ans =
1 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0
4 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!