Removing zero values from an array

조회 수: 517 (최근 30일)
Alice Stembridge
Alice Stembridge 2015년 5월 14일
댓글: Stephen23 2022년 3월 21일
I have an n x 1 array containing values. For exmaple A =
1
0
0
2
0
3
I was wondering if it was possible to create another array except without the zero values. For example
B =
1
2
3
  댓글 수: 1
Stephen23
Stephen23 2022년 3월 21일
A = [1;0;0;2;0;3]
A = 6×1
1 0 0 2 0 3
B = nonzeros(A) % Introduced before R2006a
B = 3×1
1 2 3

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

채택된 답변

Star Strider
Star Strider 2015년 5월 14일
Using logical indexing, you can calculate ‘B’ in one line:
B = A(A~=0)
  댓글 수: 6
Star Strider
Star Strider 2015년 5월 14일
Thank you Walter.
I was away for a few minutes with another Answer.
Clemens von Szczepanski
Clemens von Szczepanski 2022년 3월 21일
you can use R = rmmissing(A) now :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by