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 日

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?
Walter Roberson
Walter Roberson 2015 年 5 月 14 日
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?
Walter Roberson
Walter Roberson 2015 年 5 月 14 日
B = A(~isnan(A));
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 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by