Add a number(specific column) to vector without deleteing it.
2 次查看(过去 30 天)
显示 更早的评论
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]
0 个评论
采纳的回答
更多回答(2 个)
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
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 Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!