removing zeros element in row and colum

5 views (last 30 days)
Alya Z
Alya Z on 20 Dec 2017
Commented: Walter Roberson on 27 Dec 2017
Hi I have 200x200 of data, but it contains zeros and non-zeros element, how to remove all the zeros in the column and row?
  2 Comments
Guillaume
Guillaume on 20 Dec 2017
Note that a matrix can't have empty elements, so if you remove all the zeros you will have to reshape the matrix into a vector (Birdman's answer do that automatically) or replace the zeros with something else if you want to keep the 200x200 shape.
Walter Roberson
Walter Roberson on 27 Dec 2017
Are you trying to remove rows in which all of the elements of the row are 0? And to remove columns in which all of the elements of the column are 0 ?

Sign in to comment.

Answers (1)

Akira Agata
Akira Agata on 27 Dec 2017
As Guillaume-san mentioned, you can't have empty element in a matrix without changing the matrix shape.
But if you simply want to replace zeros in the matrix by some another value, you can do it by logical indexing technique, like:
A = eye(4); % Sample matrix with zeros
idx = A == 0; % indexing the position of zeros in the matrix
A(idx) = NaN; % Replace zeros by NaN

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!