Asked by Majid Al-Sirafi
on 24 Sep 2012

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

Answer by Daniel Shub
on 24 Sep 2012

Edited by MathWorks Support Team
on 9 Nov 2018

Accepted Answer

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
on 16 Jun 2016

Mustafa Uslu comments,

"Practical, fast and accurate!"

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
on 5 Jul 2017

b = a(a >= 2 & a <= 4); %keep 2 to 4

Sign in to comment.

Answer by Jan
on 24 Sep 2012

Edited by Jan
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.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Hassan AL Dawood (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/48938-delete-element-from-vector#comment_365955

## Hassan AL Dawood (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/48938-delete-element-from-vector#comment_365957

## SUBROTA HALDER (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/48938-delete-element-from-vector#comment_388460

## Walter Roberson (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/48938-delete-element-from-vector#comment_441296

## Walter Roberson (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/48938-delete-element-from-vector#comment_441297

## Rosie (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/48938-delete-element-from-vector#comment_466627

## Hamna Ameer (view profile)

Direct link to this comment:https://de.mathworks.com/matlabcentral/answers/48938-delete-element-from-vector#comment_488767

Sign in to comment.