Removing zero values from an array

443 ビュー (過去 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 Center および File ExchangeMATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by