필터 지우기
필터 지우기

Add a vector in another vector

조회 수: 27 (최근 30일)
hamed
hamed 2018년 5월 28일
댓글: hamed 2018년 5월 28일
Hello all,
I have a vector (for example A=rand(1,100);) and I would like to add another vector which is B=zeros(1,10); to A before each index that I want.
For instance, I would like to add vector B in vector A before index 5. The result should be a vector with 110 elements which the first four elements are the same as first four elements of A then I should have 10 zeros then all elements of vector A after the fifth element. I hope my question be clear enough.
Thanks a lot.

채택된 답변

Rik
Rik 2018년 5월 28일
The code below should work.
A=rand(1,100);
B=zeros(1,10);
wanted_index=5;
if wanted_index==1
result=[B A];
elseif wanted_index>numel(A)
result=[A B];
else
result=[A(1:(wanted_index-1)) B A(wanted_index:end)];
end
  댓글 수: 1
hamed
hamed 2018년 5월 28일
Thanks a lot Rik

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by