MATLAB Answers

delete element from vector

조회 수: 7,991(최근 30일)
Majid Al-Sirafi
Majid Al-Sirafi 2012년 9월 24일
답변: masoud sistaninejad 2021년 8월 23일
Hi everyone
how can I delete element from vector .... for example
how can I delete 3 from above vector to be
thank you
  댓글 수: 8
Jeffrey Houston
Jeffrey Houston 2021년 1월 4일
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)

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel Shub
Daniel Shub 2012년 9월 24일
편집: MathWorks Support Team 2018년 11월 9일
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) = []
  댓글 수: 7
Rik 2021년 3월 31일
@Anthony Dave Flags are not for personal bookmarks. Please remove your flag.

댓글을 달려면 로그인하십시오.

추가 답변(4개)

Jan 2012년 9월 24일
편집: Jan 2012년 9월 24일
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.
  댓글 수: 2
Joel Bay
Joel Bay 2019년 6월 28일
"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.

댓글을 달려면 로그인하십시오.

Andrei Bobrov
Andrei Bobrov 2012년 9월 24일
a = a(abs(a - 3) > eps(100))
  댓글 수: 1
Majid Al-Sirafi
Majid Al-Sirafi 2012년 9월 24일
than you very much

댓글을 달려면 로그인하십시오.

Elias Gule
Elias Gule 2015년 12월 1일
% Use logical indexing
a = a(a~=3)
  댓글 수: 2
Ntsakisi Kanyana
Ntsakisi Kanyana 2020년 3월 31일
Does it work on strings?

댓글을 달려면 로그인하십시오.

masoud sistaninejad
masoud sistaninejad 2021년 8월 23일
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

Community Treasure Hunt

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

Start Hunting!

Translated by