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

2 vues (au cours des 30 derniers jours)
Emre Doruk
Emre Doruk le 17 Jan 2023
Commenté : Dyuman Joshi le 17 Jan 2023
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 ?
  1 commentaire
Askic V
Askic V le 17 Jan 2023
Please, heave a look at function repmat:
https://www.mathworks.com/help/matlab/ref/repmat.html

Connectez-vous pour commenter.

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 17 Jan 2023
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
  2 commentaires
Emre Doruk
Emre Doruk le 17 Jan 2023
Is it still faster after codegeneartion to C?
Dyuman Joshi
Dyuman Joshi le 17 Jan 2023
I'm sorry but I don't have any idea about that.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices 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