Populate matrix with non-zero elements from previous matrix

1 view (last 30 days)
Tomás Nunes
Tomás Nunes on 13 Apr 2018
Commented: Stephen on 13 Apr 2018
I have a 7567*32 matrix in which every row has 4 non-zero elements and the rest are zeros. I want to create a 7567*4 matrix that is populated only with the non-zero elements from the previous matrix. Is there a way to do so?

Accepted Answer

Stephen on 13 Apr 2018
Edited: Stephen on 13 Apr 2018
I guess you want to keep those non-zero values in the same rows in which case try this, where M is your matrix:
Z = M.';
Z = reshape(Z(Z~=0),4,7567).'

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!