delete zeros rows and columns

조회 수: 671 (최근 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 r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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 CenterFile Exchange에서 Geometric 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