Removing zero values from an array

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 Commento

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

Accedi per commentare.

 Risposta accettata

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

6 Commenti

Thank you so much, can you do the same thing except rather than zeros it says NaN?
B = A;
B(B==0) = NaN;
What I mean't was can I remove the values which say NaN rather than if they contain a zero?
Thank you Walter.
I was away for a few minutes with another Answer.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by