필터 지우기
필터 지우기

Add a number(specific column) to vector without deleteing it.

조회 수: 1 (최근 30일)
fyza affandi
fyza affandi 2018년 11월 29일
댓글: fyza affandi 2018년 11월 29일
I have an vector
a= [1 2 3 4 5 6 7 8]
How can I add 2.5 into the vector a at any location( in this example I want it to be at column2)
so that the result will be:-
p/s: not replace , the length increases
a= [1 2.5 2 3 4 5 6 7 8]

채택된 답변

KSSV
KSSV 2018년 11월 29일
편집: KSSV 2018년 11월 29일
a= [1 2 3 4 5 6 7 8] ;
b = 2.5 ; % number to insert
pos = 2 ; % position at which number to be inserted
b = [a(1:pos-1) b a(pos:end)]

추가 답변 (2개)

dpb
dpb 2018년 11월 29일
One of many possible ways...
insertat=2;
val=2.5;
a=[a(1:insertat-1) val a(insertat:end)];
The key is to not store into the new array until the construction is done so don't change locations of expression RHS of assignment statement.

madhan ravi
madhan ravi 2018년 11월 29일
편집: madhan ravi 2018년 11월 29일
Another possibility:
a=1:8;
b=zeros(1,numel(a)+1);
insert_position=2;
b(insert_position)=2.5;
b([1 insert_position+1:end])=a(:)

카테고리

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