Effacer les filtres
Effacer les filtres

Different value putting on different columns in matrix

1 vue (au cours des 30 derniers jours)
Soumili Sen
Soumili Sen le 1 Déc 2020
Commenté : Soumili Sen le 1 Déc 2020
Hello,
I am writting a matrix
p=zeros(4,5) -----> all column values are zero
but I want different values of different column like, 1st column's every value will be 2, 2nd column's every value wil be 0.25. similarly rest columns will be assigned by other values . How I can write this code.
Thanks in advance.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 1 Déc 2020
Modifié(e) : Ameer Hamza le 1 Déc 2020
You can use repmat()
x = [2 0.25 3 1 7];
n_rows = 4;
M = repmat(x, n_rows, 1)
Result
>> M
M =
2.0000 0.2500 3.0000 1.0000 7.0000
2.0000 0.2500 3.0000 1.0000 7.0000
2.0000 0.2500 3.0000 1.0000 7.0000
2.0000 0.2500 3.0000 1.0000 7.0000
Or automatic array expansion
x = [2 0.25 3 1 7];
n_rows = 4;
M = x.*ones(n_rows,1);
Both are equivalent.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing 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