How do you initialize an N*M matrix with certain N*1 vector?

3 次查看(过去 30 天)
I had a martix N*M matrix, I try to init matrix with an vector. I am doing with code which below.
signal=zeros(5,4);
vectorA=[1 2 3 4];
for k=1:5
signal(k,:)=vectorA;
end
Is there better way to a work this code ?

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-1-17
You can use repmat()
signal=zeros(5,4);
vectorA=[1 2 3 4];
for k=1:5
signal(k,:)=vectorA;
end
signal
signal = 5×4
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
y=repmat(vectorA,5,1)
y = 5×4
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by