delete element from vector

4,840 views (last 30 days)
Majid Al-Sirafi
Majid Al-Sirafi on 24 Sep 2012
Edited: Abdul samad on 4 Aug 2023
Hi everyone
how can I delete element from vector .... for example
how can I delete 3 from above vector to be
thank you
Rosie on 5 Jul 2017
Edited: Walter Roberson 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
Edited: Hamna Ameer on 29 Sep 2017
a(3)=[] how can i directly store this in a new vector say b?

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) = []
Walter Roberson
Walter Roberson on 5 Jul 2017
b = a(a >= 2 & a <= 4); %keep 2 to 4
Rik on 31 Mar 2021
@Anthony Dave Flags are not for personal bookmarks. Please remove your flag.

Sign in to comment.

More Answers (6)

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.
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.
irvin rynning
irvin rynning on 6 Dec 2021
unfortunately some of us prefer to use Matlab to solve problems in a timely manner, and cannot always engage in stackover-flow style plaudits on criticizing one's peers

Sign in to comment.

masoud sistaninejad
masoud sistaninejad on 23 Aug 2021
A = [ 1 2 3 4 5 6 7]
A = 1×7
1 2 3 4 5 6 7
B = [1 3 6]
B = 1×3
1 3 6
C = setdiff(A,B)
C = 1×4
2 4 5 7
Andy Rojas
Andy Rojas on 24 Nov 2021
Thank you!
Emma Fickett
Emma Fickett on 29 Oct 2022
I've scoured through so many forums trying to remove a vector of values from another vector and setdiff does exactly what I needed, thank you so much!!

Sign in to comment.

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

Will Reeves
Will Reeves on 15 Feb 2022
really crude, but if you wanted to remove a row defined by and index, rather than a value, you could do something like this:
function out=removeRow(in,index)
% removes a row from an matrix
if index>n || index<0
error('index needs to be within the range of the data')
if n==1
out=[]; % you've removed the last entry
% strip out the required entry
if index==1
elseif index==n
out=in([1:index-1 index+1:n]);

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.

Abdul samad
Abdul samad on 4 Aug 2023
Edited: Abdul samad on 4 Aug 2023
Yes , you can delete 3 from the given array by assigning the null matrix, like this .
In the command window do like this.
>> a=[1,2,3,4,5];
>> a(3) = [ ];
This will delete the 3 from the array a = [1,2,3,4,5];
Thank You


Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!