Borrar filtros
Borrar filtros

Different value putting on different columns in matrix

1 visualización (últimos 30 días)
Soumili Sen
Soumili Sen el 1 de Dic. de 2020
Comentada: Soumili Sen el 1 de Dic. de 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.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 1 de Dic. de 2020
Editada: Ameer Hamza el 1 de Dic. de 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.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by