Removing zero values from an array

412 Ansichten (letzte 30 Tage)
Alice Stembridge
Alice Stembridge am 14 Mai 2015
Kommentiert: Stephen23 am 21 Mär. 2022
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 Kommentar
Stephen23
Stephen23 am 21 Mär. 2022
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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 14 Mai 2015
Using logical indexing, you can calculate ‘B’ in one line:
B = A(A~=0)
  6 Kommentare
Star Strider
Star Strider am 14 Mai 2015
Thank you Walter.
I was away for a few minutes with another Answer.
Clemens von Szczepanski
Clemens von Szczepanski am 21 Mär. 2022
you can use R = rmmissing(A) now :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by