Azzera filtri
Azzera filtri

Different value putting on different columns in matrix

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Ameer Hamza
Ameer Hamza il 1 Dic 2020
Modificato: Ameer Hamza il 1 Dic 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.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by