removing zeros element in row and colum

5 ビュー (過去 30 日間)
Alya Z
Alya Z 2017 年 12 月 20 日
コメント済み: Walter Roberson 2017 年 12 月 27 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by