removing zeros element in row and colum

5 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2017-12-20
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 2017-12-27
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 ?

请先登录,再进行评论。

回答(1 个)

Akira Agata
Akira Agata 2017-12-27
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by