MATLAB Answers

delete element from vector

8,283 views (last 30 days)
Hi everyone
how can I delete element from vector .... for example
how can I delete 3 from above vector to be
thank you


Show 5 older comments
Rosie on 5 Jul 2017
Hi majed
You can use the follwoing
the number will delete
Good luck
Hamna Ameer
Hamna Ameer on 29 Sep 2017
a(3)=[] how can i directly store this in a new vector say b?
Jeffrey Houston
Jeffrey Houston on 4 Jan 2021
Hi Hamna, sorry for the late response, but I'll answer for others anyways. The line of code below will do the trick for what you're asking for.
b = a(3)

Sign in to comment.

Accepted Answer

Daniel Shub
Daniel Shub on 24 Sep 2012
Edited: MathWorks Support Team on 9 Nov 2018
I can think of three ways that are all slightly different
If you want to get rid of all cases where a is exactly equal to 3
b = a(a~=3);
If you want to delete the third element
b = a;
b(3) = [];
or on a single line
b = a([1:2, 4:end]);
Or, as Jan suggests:
a = [2,3,1,5,4]
a(a == 3) = []


Show 3 older comments
kwabena boafo-mensah
kwabena boafo-mensah on 8 Jul 2016
how does this work when i need to delete a range of row elements from a vector
Walter Roberson
Walter Roberson on 5 Jul 2017
b = a(a >= 2 & a <= 4); %keep 2 to 4
Arjun Siva S
Arjun Siva S on 10 Dec 2020
vera level ponga !

Sign in to comment.

More Answers (3)

Jan on 24 Sep 2012
Edited: Jan on 24 Sep 2012
a = [1,2,3,4,5]
a(3) = []
a = [2,3,1,5,4]
a(a == 3) = []
These methods are explained exhaustively in the "Getting Started" chapters of the documentation. It is strongly recommended to read them completely. The forum is not though to explain the fundamental basics. Thanks.


Majid Al-Sirafi
Majid Al-Sirafi on 24 Sep 2012
thank you very much
Joel Bay
Joel Bay on 28 Jun 2019
"These methods are explained exhaustively in the "Getting Started" chapters of the documentation."
Wrong, definetely not exhaustively after comparing Daniel's answer and the documentation. Logical indexing is not even mentioned. The answers to this question is still useful in 2019.

Sign in to comment.

Andrei Bobrov
Andrei Bobrov on 24 Sep 2012
a = a(abs(a - 3) > eps(100))

  1 Comment

Majid Al-Sirafi
Majid Al-Sirafi on 24 Sep 2012
than you very much

Sign in to comment.

Elias Gule
Elias Gule on 1 Dec 2015
% Use logical indexing
a = a(a~=3)


denny on 31 Aug 2017
I like this answer.
Ntsakisi Kanyana
Ntsakisi Kanyana on 31 Mar 2020
Does it work on strings?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!