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

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일

5 개 추천

Using logical indexing, you can calculate ‘B’ in one line:
B = A(A~=0)

댓글 수: 6

Alice Stembridge
Alice Stembridge 2015년 5월 14일
Thank you so much, can you do the same thing except rather than zeros it says NaN?
B = A;
B(B==0) = NaN;
Alice Stembridge
Alice Stembridge 2015년 5월 14일
What I mean't was can I remove the values which say NaN rather than if they contain a zero?
B = A(~isnan(A));
Star Strider
Star Strider 2015년 5월 14일
Thank you Walter.
I was away for a few minutes with another Answer.
you can use R = rmmissing(A) now :)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

태그

질문:

2015년 5월 14일

댓글:

2022년 3월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by