delete zeros rows and columns

521 次查看(过去 30 天)
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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 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?

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by