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

조회 수: 1 (최근 30일)
Emre Doruk
Emre Doruk 2023년 1월 17일
댓글: Dyuman Joshi 2023년 1월 17일
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
Askic V
Askic V 2023년 1월 17일
Please, heave a look at function repmat:
https://www.mathworks.com/help/matlab/ref/repmat.html

댓글을 달려면 로그인하십시오.

채택된 답변

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
  댓글 수: 2
Emre Doruk
Emre Doruk 2023년 1월 17일
Is it still faster after codegeneartion to C?
Dyuman Joshi
Dyuman Joshi 2023년 1월 17일
I'm sorry but I don't have any idea about that.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by