Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Removing rows containing all zeros in a matrix

Subject: Removing rows containing all zeros in a matrix

From: Rahul Singhal

Date: 7 May, 2010 19:33:06

Message: 1 of 14

Hi All,
   I have a matrix containing 1's and 0's. I want to remove the rows that contain all 0's. Is there any matlab function that can do this.

Can anybody help me in this.

Thanks
Rahul Singhal

Subject: Removing rows containing all zeros in a matrix

From: us

Date: 7 May, 2010 19:43:05

Message: 2 of 14

"Rahul Singhal" <rsinghalomatic@gmail.com> wrote in message <hs1pti$hmk$1@fred.mathworks.com>...
> Hi All,
> I have a matrix containing 1's and 0's. I want to remove the rows that contain all 0's. Is there any matlab function that can do this.
>
> Can anybody help me in this.
>
> Thanks
> Rahul Singhal

one of the solutions

     m=[
          1 2 3
          0 4 5
          0 0 0
          6 0 0
     ];
     m(all(m==0,2),:)=[]
%{
% m =
     1 2 3
     0 4 5
     6 0 0
%

us

Subject: Removing rows containing all zeros in a matrix

From: Anthony Hopf

Date: 13 Jul, 2010 18:04:04

Message: 3 of 14

us, very cool. If you want to get rid of any row that has 0's in it

m =

     1 2 3
     0 4 5
     0 0 0
     6 0 0

>> m(any(m==0,2),:)=[]

m =

     1 2 3



"us " <us@neurol.unizh.ch> wrote in message <hs1qg9$psa$1@fred.mathworks.com>...
> "Rahul Singhal" <rsinghalomatic@gmail.com> wrote in message <hs1pti$hmk$1@fred.mathworks.com>...
> > Hi All,
> > I have a matrix containing 1's and 0's. I want to remove the rows that contain all 0's. Is there any matlab function that can do this.
> >
> > Can anybody help me in this.
> >
> > Thanks
> > Rahul Singhal
>
> one of the solutions
>
   m=[
           1 2 3
         0 4 5
          0 0 0
           6 0 0
      ];
> m(all(m==0,2),:)=[]
> %{
> % m =
> 1 2 3
> 0 4 5
> 6 0 0
> %
>
> us

Subject: Removing rows containing all zeros in a matrix

From: Anthony Hopf

Date: 13 Jul, 2010 18:17:05

Message: 4 of 14

But what if you want to replace the values?

m(any(m ==0,2),:)=[1 1 1];

This doesn't work, I get a dimension mismatch problem.

Thank you


"us " <us@neurol.unizh.ch> wrote in message <hs1qg9$psa$1@fred.mathworks.com>...
> "Rahul Singhal" <rsinghalomatic@gmail.com> wrote in message <hs1pti$hmk$1@fred.mathworks.com>...
> > Hi All,
> > I have a matrix containing 1's and 0's. I want to remove the rows that contain all 0's. Is there any matlab function that can do this.
> >
> > Can anybody help me in this.
> >
> > Thanks
> > Rahul Singhal
>
> one of the solutions
>
> m=[
> 1 2 3
> 0 4 5
> 0 0 0
> 6 0 0
> ];
> m(all(m==0,2),:)=[]
> %{
> % m =
> 1 2 3
> 0 4 5
> 6 0 0
> %
>
> us

Subject: Removing rows containing all zeros in a matrix

From: Andy

Date: 13 Jul, 2010 19:06:19

Message: 5 of 14

"Anthony Hopf" <anthony.hopf@gmail.com> wrote in message <i1iaj1$d8k$1@fred.mathworks.com>...
> But what if you want to replace the values?
>
> m(any(m ==0,2),:)=[1 1 1];
>
> This doesn't work, I get a dimension mismatch problem.
>
> Thank you


m=[
  1 2 3
  0 4 5
  0 0 0
  6 0 0
];
n = any(m==0,2);
m(n,:)=repmat([1 1 1],[sum(n) 1]);

You got dimension mismatch because you were trying to assign a 1x3 array to a 3x3 array. You need to ensure the data you're inserting is the same size as the data you're editing.

Subject: Removing rows containing all zeros in a matrix

From: Hanah

Date: 25 Aug, 2010 22:19:04

Message: 6 of 14

Hello Us,

I have a question that follows a logic corollary to the original question. I have loaded some alphanumeric data into MATLAB using the 'textscan' function. The problem is that I have to analyze this data in a format similar to pivot tables in excel.

What is the easiest way to select rows from a cell array that contain a specific number or text in a given column? For example:

%cell array "input{}"...
input = [
12 'cats' 2 'girls'
17 'dogs' 5 'boys'
23 'cats' 2 'boys'
];

%select only rows containing the string 'cats' in the second column...
output_1 = [
12 'cats' 2 'girls'
23 'cats' 4 'boys'
];

%select only rows containing the number 2 in the third column...
output_2 = [
12 'cats' 2 'girls'
23 'cats' 2 'boys'
];

%select only rows containing the number 17 in the first column...
output_3 = [
17 'dogs' 5 'boys'
];

%etc...

This is for a very large set of data where computation time is important, so if there is a function/notation easily achieve this and prevent looping, that would be excellent. I have tried 'grpstats' and 'accumarray' but can't seem to crack it.

Thank you so much for your time. I really appreciate your help!

- Han

Subject: Removing rows containing all zeros in a matrix

From: Kwen

Date: 14 Aug, 2012 14:47:06

Message: 7 of 14

"us" wrote in message <hs1qg9$psa$1@fred.mathworks.com>...
> "Rahul Singhal" <rsinghalomatic@gmail.com> wrote in message <hs1pti$hmk$1@fred.mathworks.com>...
> > Hi All,
> > I have a matrix containing 1's and 0's. I want to remove the rows that contain all 0's. Is there any matlab function that can do this.
> >
> > Can anybody help me in this.
> >
> > Thanks
> > Rahul Singhal
>
> one of the solutions
>
> m=[
> 1 2 3
> 0 4 5
> 0 0 0
> 6 0 0
> ];
> m(all(m==0,2),:)=[]
> %{
> % m =
> 1 2 3
> 0 4 5
> 6 0 0
> %
>
> us

This works for the method I'm trying to use, but I also want to index where those zeros were in order to adjust the corresponding 'x' and interpolate to create a new data set. How would I go about doing so?

Thanks!!

Subject: Removing rows containing all zeros in a matrix

From: Bruno Luong

Date: 14 Aug, 2012 15:22:09

Message: 8 of 14

"Kwen " <kwenami@hotmail.com> wrote in message <k0doda$cqf$1@newscl01ah.mathworks.com>...

>
> This works for the method I'm trying to use, but I also want to index where those zeros were [...]

doc find

Bruno

Subject: Removing rows containing all zeros in a matrix

From: dpb

Date: 14 Aug, 2012 17:57:33

Message: 9 of 14

On 8/14/2012 9:47 AM, Kwen wrote:
...

>> > ... I want to remove the rows that contain all 0's. ...
...

>> one of the solutions
>>
...

>> m(all(m==0,2),:)=[]
...

> This works for the method I'm trying to use, but I also want to index
> where those zeros were in order to adjust the corresponding 'x' and
> interpolate to create a new data set. How would I go about doing so?

Just use the same logic vector on the corollary arrays as well...

idx=all(m==0,2);
m(idx,:)= []; % remove same rows from m and x...
x(idx,:)= []; % remove same rows from m and x...

--

Subject: Removing rows containing all zeros in a matrix

From: Bruno Luong

Date: 14 Aug, 2012 18:04:07

Message: 10 of 14

dpb <none@non.net> wrote in message <k0e3ih$gvs$1@speranza.aioe.org>...
> On 8/14/2012 9:47 AM, Kwen wrote:

>
> Just use the same logic vector on the corollary arrays as well...
>
> idx=all(m==0,2);
> m(idx,:)= []; % remove same rows from m and x...
> x(idx,:)= []; % remove same rows from m and x...
>

But it's hard to see how one can use logical indexing for interpolation.

Bruno

Subject: Removing rows containing all zeros in a matrix

From: dpb

Date: 14 Aug, 2012 18:16:13

Message: 11 of 14

On 8/14/2012 1:04 PM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <k0e3ih$gvs$1@speranza.aioe.org>...
>> On 8/14/2012 9:47 AM, Kwen wrote:
>
>>
>> Just use the same logic vector on the corollary arrays as well...
>>
>> idx=all(m==0,2);
>> m(idx,:)= []; % remove same rows from m and x...
>> x(idx,:)= []; % remove same rows from m and x...
>>
>
> But it's hard to see how one can use logical indexing for interpolation.

'Pends on what OP's up to, specifically...if it's to use the indices as
index, agreed; just pointing out that the logical array isn't limited to
operating on the one array if making them consonant in size is the
objective.

--

Subject: Removing rows containing all zeros in a matrix

From: Bruno Luong

Date: 14 Aug, 2012 19:45:08

Message: 12 of 14

dpb <none@non.net> wrote in message <k0e4lb$u73$1@speranza.aioe.org>...

>
> 'Pends on what OP's up to, specifically...

OP's earlier description

[ ... but I also want to index where those zeros were in order to adjust the corresponding 'x' and interpolate to create a new data set... ]

seems unambiguous to me.

Bruno

Subject: Removing rows containing all zeros in a matrix

From: dpb

Date: 14 Aug, 2012 19:51:54

Message: 13 of 14

On 8/14/2012 2:45 PM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <k0e4lb$u73$1@speranza.aioe.org>...
>
>>
>> 'Pends on what OP's up to, specifically...
>
> OP's earlier description
>
> [ ... but I also want to index where those zeros were in order to adjust
> the corresponding 'x' and interpolate to create a new data set... ]
>
> seems unambiguous to me.

Whatever if you're bound to be certain you're "right"...I'm not going to
argue one way or another--as said simply pointed out _IF_ (the
proverbial "big if") the intent were to match x and m there's nothing
magic about the index applying to m only. Certainly there's often far
more basic lack of clarity in queries at cssm than this simply owing to
terminology chosen vs meaning.

--

Subject: Removing rows containing all zeros in a matrix

From: Cristian

Date: 3 Jan, 2013 03:33:13

Message: 14 of 14

Simple and 100% effective, Thanks!!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us