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

采纳的回答

Stephen23
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 个评论
Stephen23
Stephen23 2018-1-7
编辑:Stephen23 2018-1-7
@Prabha Kumaresan: if this is what you need then please remember to accept the answer that helps you most. We are volunteers, and accepting is the easiest way for you to show your appreciation for their time spent helping you.

请先登录,再进行评论。

更多回答(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