Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
how to generate the matrix of (50,100) with its diagonal alone having values

Réponse acceptée

Stephen23
Stephen23 le 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 commentaires
Prabha Kumaresan
Prabha Kumaresan le 7 Jan 2018
thanks this was the result i expected.
Stephen23
Stephen23 le 7 Jan 2018
Modifié(e) : Stephen23 le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operating on Diagonal Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by