## delete element from vector

Majid Al-Sirafi

### Majid Al-Sirafi (view profile)

さんによって質問されました 2012 年 9 月 24 日

### Joel Bay (view profile)

さんによって コメントされました 2019 年 6 月 28 日
Daniel Shub

### Daniel Shub (view profile)

さんの 回答が採用されました
Hi everyone
how can I delete element from vector .... for example
a=[1,2,3,4,5]
how can I delete 3 from above vector to be
a=[1,2,4,5]
thank you
majid

Walter Roberson

on 29 Mar 2017
a(3) = [];
Rosie

### Rosie (view profile)

on 5 Jul 2017
Hi majed
You can use the follwoing
a(index)=[]
a(3)=[]
the number will delete
Good luck
Hamna Ameer

### Hamna Ameer (view profile)

on 29 Sep 2017
a(3)=[] how can i directly store this in a new vector say b?

サインイン to comment.

## 4 件の回答

### Daniel Shub (view profile)

Answer by Daniel Shub

### Daniel Shub (view profile)

on 24 Sep 2012
Edited by MathWorks Support Team

### MathWorks Support Team (view profile)

on 9 Nov 2018

I can think of three ways that are all slightly different
a=[1,2,3,4,5];
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 (view profile)

on 16 Jun 2016
"Practical, fast and accurate!"
kwabena boafo-mensah

### kwabena boafo-mensah (view profile)

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 (view profile)

on 5 Jul 2017
b = a(a >= 2 & a <= 4); %keep 2 to 4

サインイン to comment.

on 24 Sep 2012
Edited by Jan

### Jan (view profile)

on 24 Sep 2012

a = [1,2,3,4,5]
a(3) = []
Or:
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 (view profile)

on 24 Sep 2012
thank you very much
Joel Bay

### Joel Bay (view profile)

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.

サインイン to comment.

### Andrei Bobrov (view profile)

Answer by Andrei Bobrov

### Andrei Bobrov (view profile)

on 24 Sep 2012

a = a(abs(a - 3) > eps(100))

Majid Al-Sirafi

### Majid Al-Sirafi (view profile)

on 24 Sep 2012
than you very much

サインイン to comment.

### Elias Gule (view profile)

Answer by Elias Gule

### Elias Gule (view profile)

on 1 Dec 2015

% Use logical indexing
a = a(a~=3)

denny

### denny (view profile)

on 31 Aug 2017
I like this answer.

サインイン to comment.

Translated by