Removing zero values from an array

488 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 21 Mar 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

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 14 May 2015
Using logical indexing, you can calculate ‘B’ in one line:
B = A(A~=0)
  6 Comments
Star Strider
Star Strider on 14 May 2015
Thank you Walter.
I was away for a few minutes with another Answer.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!