Clear Filters
Clear Filters

how to generate the matrix of (50,100) with its diagonal alone having values

2 views (last 30 days)
how to generate the matrix of (50,100) with its diagonal alone having values

Accepted Answer

Stephen23
Stephen23 on 7 Jan 2018
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 Comments
Stephen23
Stephen23 on 7 Jan 2018
Edited: Stephen23 on 7 Jan 2018
@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.

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!