# delete zeros rows and columns

552 visualizzazioni (ultimi 30 giorni)
Andrea il 31 Mag 2012
Commentato: Mads Albertsen il 17 Ago 2021
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!
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Walter Roberson il 31 Mag 2012
data( ~any(data,2), : ) = []; %rows
data( :, ~any(data,1) ) = []; %columns
##### 12 CommentiMostra 10 commenti meno recentiNascondi 10 commenti meno recenti
Walter Roberson il 17 Ago 2021

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 il 17 Ago 2021
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

Accedi per commentare.

### Più risposte (2)

Geoff il 31 Mag 2012
Yeah you did too much!
% Remove zero rows
data( all(~data,2), : ) = [];
% Remove zero columns
data( :, all(~data,1) ) = [];
##### 5 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Giuseppe il 4 Ago 2018
Modificato: Giuseppe il 4 Ago 2018
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 il 23 Giu 2020
Thank you, it worked :)
Can someone explain why this works.
Regards,
Olaf

Accedi per commentare.

Rubina Easmin il 10 Feb 2020
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 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Rubina Easmin il 15 Feb 2020
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 il 15 Feb 2020
How do I crop every single character automatically in matlab by using segmentation?

Accedi per commentare.

### Categorie

Scopri di più su Geometric Transformation and Image Registration in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by