フィルターのクリア

delete zeros rows and columns

478 ビュー (過去 30 日間)
Andrea
Andrea 2012 年 5 月 31 日
コメント済み: Mads Albertsen 2021 年 8 月 17 日
I try this code to delete all row and column with all zero values in them. It simply works for deleting the columns with all zero values abut it does not work for rows! Can anybody please help me?
data=[0 0 0 0 0 0 0 0; 0 0 2 3 4 0 1 0; 0 0 1 2 3 0 0 0];
data( all( ~any( data), 2 ), : ) = []; % removes all rows with all zero
data( :, all( ~any( data ), 1 ) ) = []; % and columns
I mean the first line (% removes all rows with all zero) does not work!

採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 31 日
data( ~any(data,2), : ) = []; %rows
data( :, ~any(data,1) ) = []; %columns
  12 件のコメント
Walter Roberson
Walter Roberson 2021 年 8 月 17 日

Mads Albertson:

Should the column be removed if it contains at least one zero, or should it be removed only if it is all zero? Based on your wanting to remove corresponding rows, it would only seem to make sense if it was at least one zero (otherwise you would be removing all rows)

It might be possible to write a single expression that removed the rows and columns, but it would have to evaluate the test for zero twice, except possibly it could be written as a single line of code if it were permitted to use auxiliary functions. The version that evaluates the zero test twice twice is

data = data(all(data, 2), all(data, 1));
Mads Albertsen
Mads Albertsen 2021 年 8 月 17 日
it doesnt need to be a full row if 0's, just if one occour in a row/ column, but your code works perfect
thx Walter Roberson

サインインしてコメントする。

その他の回答 (2 件)

Geoff
Geoff 2012 年 5 月 31 日
Yeah you did too much!
% Remove zero rows
data( all(~data,2), : ) = [];
% Remove zero columns
data( :, all(~data,1) ) = [];
  5 件のコメント
Giuseppe
Giuseppe 2018 年 8 月 4 日
編集済み: Giuseppe 2018 年 8 月 4 日
Guys, what if we would need to remove zero rows from the first occurence onwards? E.g. at same point all rows are zeros but they may be non zero afterwards still you want to eliminate all rows from the first zero occurence?
Olaf van Buul
Olaf van Buul 2020 年 6 月 23 日
Thank you, it worked :)
Can someone explain why this works.
Regards,
Olaf

サインインしてコメントする。


Rubina Easmin
Rubina Easmin 2020 年 2 月 10 日
I'm new to MATLAB, I want to delete the entire rows and columns which contain all of ones in a binary image.I want to keep rows and columns if that contain only single zero. Any tips on how to go about doing this? Thanks
>> binaryimage
binaryimage =
33×35 logical array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 0 0 1
1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1
1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1
1 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1
1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 0 0 1 1 1 0 0 1
1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0 0 1
1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 1
1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1
1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1
1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 1
1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 1
1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1
1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 1 0 0 1
1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
I want to remove rows and columns that contains all of ones
  4 件のコメント
Rubina Easmin
Rubina Easmin 2020 年 2 月 15 日
I want to crop a binary image by using segmentation algorithm.If I want to solve this problem by doing segmentation,how can I implement the code?
how do I find source code/algorithms of any existing papers that they implement?
Rubina Easmin
Rubina Easmin 2020 年 2 月 15 日
How do I crop every single character automatically in matlab by using segmentation?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGeometric Transformation and Image Registration についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by