1,024 views (last 30 days)

Hi everyone

now, how can I insert element in vector .... for example

a=[1,2,4,5]

how can I embed 3 between 2 and 4 in above vector to be

a=[1,2,3,4,5]

thank you

majid

Daniel Shub
on 24 Sep 2012

While I think this is a homework problem ...

Function handles and cat are your friends

insert = @(a, x, n)cat(2, x(1:n), a, x(n+1:end));

insert(3, [1,2,4,5], 2)

ans = 1 2 3 4 5

Jonathan Campelli
on 12 Mar 2015

Here is an application specific solution:

a=[1 2 4 5] %Your predefined vector.

a=sort([a 3]) %The "[a 3]" operation adds the element 3 to the end of vector "a", creating vector [1 2 4 5 3]. "Sort()" then alligns the new vector's elements in order from least to greatest.

Maximilian Sgodda
on 7 Aug 2020

Thanks Jonathan!

I had a slightly different problem, like the question was asked and could solve it with your code :)

Andrei Bobrov
on 24 Sep 2012

Edited: Andrei Bobrov
on 24 Sep 2012

b = 3;

i1 = 3;

a = [a(1:i1-1),b,a(i1:end)];

or

a = [1 2 4 5];

i1 = 3;

b = 3000;

n = numel(b);

out = ones(size(a) + [0 n]);

out(i1 + (0:n-1)) = 0;

out(out > 0) = a;

out(out == 0) = b;

Bernard
on 29 Jul 2019

a = [1 2 4 5];

b = 3;

idx = 3;

a = [a(1:length(a) < idx), b, a(1:length(a) >= idx)];

Simpler version of your second example, which I prefer because it works with idx == 1 or idx == length(a).

Walter Roberson
on 7 Mar 2018

There is no MATLAB operator for inserting into a MATLAB vector. Concatenating elements as described by Wayne King, Andrei Bobrov, and Daniel Shub is the natural MATLAB solution to this task.

The only exception to this is MATLAB String objects (R2016b and later), which have insertBefore and insertAfter operations defined for them that search the input strings to match a given text and insert at that point.

I have attached code to implement insertion after a given point into generalized vectors. Generalized vectors here refers to the fact that the vectors might have 3 or more dimensions, and that the code is not restricted to numeric or char.

There are some important differences between this code and the ones posted above:

- this code handles vectors of any dimension, not just row vectors
- the output is the same class as the initial vector even if different data types are involved. For example the other implementations if asked to insert 'a' after (double) 50 would produce '2a' because [50 'a'] automatically converts the double to char because of MATLAB rules about converting to the most restrictive data type when cat() is used. The code I attached will produce [50 97] instead -- but if you ask to insert (double) 50 before 'a' then you will get '2a' because the data type of the original vector is retained
- However, an empty original array will cause the output to be in the type of the new data so that you can always use [] to indicate empty array no matter what type you are appending

There are a number of design decisions explicitly documented in the code -- this seemingly simple operation is surprisingly complex.

Elena Fiermonte
on 30 Sep 2018

Edited: Elena Fiermonte
on 6 Oct 2018

Hi everyone. I know it's a bit long, this should work with every kind of sorted vector. Feel free to refine it. Here it is:

%code A=[1 2 4 5]; % you must predefined a sorted vector. B=zeros(1,length(A)+1); L=length(A); n=input('ins num: ') for i=1:L for j=i:L if n>A(i) B(i+1)=n; B(i)=A(i); B(j+1)=A(j); elseif n==A(1) B(1:2)=A(1); B(i+1)=A(i); end end end B

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

Start Hunting!
## 10 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_101010

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_101010

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_101011

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_101011

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_101013

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_101013

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_271598

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_271598

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_271611

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_271611

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_271943

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_271943

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_271994

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_271994

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_529363

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_529363

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_529365

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_529365

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_529386

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/48942-insert-element-in-vector#comment_529386

Sign in to comment.