Different value putting on different columns in matrix

1 次查看(过去 30 天)
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.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-12-1
编辑:Ameer Hamza 2020-12-1
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by