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:
remove 0's

Subject: remove 0's

From: Vihang Patil

Date: 10 Dec, 2011 13:32:09

Message: 1 of 10

Hello,
I have an 10 x m matrix. In this matrix, I sometimes get an entire column of 0. I want to remove this 0 from the matrix . Something like the following
0 0 0 0 0.12 0.12 0.14 0.16 0 0 0.17 0.18 0.18 0.2
0 0 0 0 2.4 2.5 2.6 2.7 0 0 2.9 3 3.1 3.2
0 0 0 0 2 2.1 2.2 2.3 0 0 2.4 2.2 2 1.8
0 0 0 0 5.8 5.9 6 6.1 0 0 5.8 5.9 6 6.1
0 0 0 0 32756 32756 32754 32752 0 0 32756 32756 32754 32752
0 0 0 0 125 125 128 129 0 0 125 125 128 129
0 0 0 0 65536 65536 65535 65534 0 0 65536 65536 65535 65534
0 0 0 0 0 0 1 2 0 0 0 0 1 2
0 0 0 0 65536 65536 65536 65536 0 0 65534 65533 65532 65531
0 0 0 0 0 0 0 0 0 0 2 3 4 5


I want to only remove the zeros that are present in the entire column. If there is 0 in a column but other non zero real numbers are there in that particular column, then I wish to keep the 0.
Please guide. I need sometime of vectorised code that will execute fast as this is in a Data Acquistion Process.

Thanks

Vihang

Subject: remove 0's

From: dpb

Date: 10 Dec, 2011 14:17:01

Message: 2 of 10

On 12/10/2011 7:32 AM, Vihang Patil wrote:
> Hello,
> I have an 10 x m matrix. In this matrix, I sometimes get an entire
> column of 0. I want to remove this 0 from the matrix...
...
> I want to only remove the zeros that are present in the entire column.
> If there is 0 in a column but other non zero real numbers are there in
> that particular column, then I wish to keep the 0....

x(~all(x),:)=[];

--

Subject: remove 0's

From: dpb

Date: 10 Dec, 2011 14:16:25

Message: 3 of 10

On 12/10/2011 7:32 AM, Vihang Patil wrote:
> Hello,
> I have an 10 x m matrix. In this matrix, I sometimes get an entire
> column of 0. I want to remove this 0 from the matrix...
...
> I want to only remove the zeros that are present in the entire column.
> If there is 0 in a column but other non zero real numbers are there in
> that particular column, then I wish to keep the 0....

x(all(x),:)=[];

--

Subject: remove 0's

From: dpb

Date: 10 Dec, 2011 14:45:39

Message: 4 of 10

On 12/10/2011 8:16 AM, dpb wrote:
> On 12/10/2011 7:32 AM, Vihang Patil wrote:
>> Hello,
>> I have an 10 x m matrix. In this matrix, I sometimes get an entire
>> column of 0. I want to remove this 0 from the matrix...
> ....
>> I want to only remove the zeros that are present in the entire column.
>> If there is 0 in a column but other non zero real numbers are there in
>> that particular column, then I wish to keep the 0....
>
> x(all(x),:)=[];

Dang, thought I stopped this one...it's missing the negation operator...

--

Subject: remove 0's

From: Vihang Patil

Date: 10 Dec, 2011 15:41:10

Message: 5 of 10

dpb <none@non.net> wrote in message <jbvrag$ci7$1@speranza.aioe.org>...
> On 12/10/2011 8:16 AM, dpb wrote:
> > On 12/10/2011 7:32 AM, Vihang Patil wrote:
> >> Hello,
> >> I have an 10 x m matrix. In this matrix, I sometimes get an entire
> >> column of 0. I want to remove this 0 from the matrix...
> > ....
> >> I want to only remove the zeros that are present in the entire column.
> >> If there is 0 in a column but other non zero real numbers are there in
> >> that particular column, then I wish to keep the 0....
> >
> > x(all(x),:)=[];
>
> Dang, thought I stopped this one...it's missing the negation operator...
>
> --

I tried your code, but I get the following error

x(all(x),:)=[];
??? Index of element to remove exceeds matrix dimensions.

x(~all(x),:)=[];
??? Index of element to remove exceeds matrix dimensions.

Vihang

Subject: remove 0's

From: Nasser M. Abbasi

Date: 10 Dec, 2011 17:48:41

Message: 6 of 10

On 12/10/2011 9:41 AM, Vihang Patil wrote:
> dpb<none@non.net> wrote in message<jbvrag$ci7$1@speranza.aioe.org>...
>> On 12/10/2011 8:16 AM, dpb wrote:
>>> On 12/10/2011 7:32 AM, Vihang Patil wrote:
>>>> Hello,
>>>> I have an 10 x m matrix. In this matrix, I sometimes get an entire
>>>> column of 0. I want to remove this 0 from the matrix...
>>> ....
>>>> I want to only remove the zeros that are present in the entire column.
>>>> If there is 0 in a column but other non zero real numbers are there in
>>>> that particular column, then I wish to keep the 0....
>>>
>>> x(all(x),:)=[];
>>
>> Dang, thought I stopped this one...it's missing the negation operator...
>>
>> --
>
> I tried your code, but I get the following error
>
> x(all(x),:)=[];
> ??? Index of element to remove exceeds matrix dimensions.
>
> x(~all(x),:)=[];
> ??? Index of element to remove exceeds matrix dimensions.
>
> Vihang

I do not know what you typed there. Better to show all
the code you used.

But any way, this works on my system 2011a:

EDU>> A=[1 2 0;2 3 0; 4 5 0]
A =
      1 2 0
      2 3 0
      4 5 0

EDU>> A(:,~all(A,1))=[]
A =
      1 2
      2 3
      4 5
EDU>>

--Nasser

Subject: remove 0's

From: dpb

Date: 10 Dec, 2011 17:53:09

Message: 7 of 10

On 12/10/2011 9:41 AM, Vihang Patil wrote:
...

> I tried your code, but I get the following error
>
> x(all(x),:)=[];
> ??? Index of element to remove exceeds matrix dimensions.
>
> x(~all(x),:)=[];
> ??? Index of element to remove exceeds matrix dimensions.
...

Dang, sorry, I reversed the order from what you want/need...

x(:,~all(x))=[];

--

Subject: remove 0's

From: dpb

Date: 10 Dec, 2011 18:23:48

Message: 8 of 10

On 12/10/2011 9:41 AM, Vihang Patil wrote:
...

> I tried your code, but I get the following error
...

> x(~all(x),:)=[];
> ??? Index of element to remove exceeds matrix dimensions.
...

I (and Nasser) provided the fix; I came back to point out some useful
tips for solving such issues in the future w/o reliance on cs-sm...

First, start w/

doc all

if you don't know for absolute certain what it does, _exactly_ for a
matrix argument. It will point out in this case that it returns a
logical vector of _columns_ in the array that satisfy the test. That
should be the first clue that I inadvertently wrote the (air) code
(sorry, Steve, I should have tested a case :) ) indexing expression
backwards and put the column indices before the desired row expression
(':' in this case).

If that still weren't enough, then start from the inside out of an
expression such as the above. What do the expressions all(x) and
~all(x) return at the command line (or in the debugger if you're at a
lower level in a function)? There you would find it is the logical row
vector of the same size as the columns. That ought to finally be enough...

The point is, be logical and look at what you're doing rather than
trying to just throw a solution at it you don't understand.

--

Subject: remove 0's

From: Vihang Patil

Date: 16 Dec, 2011 14:01:08

Message: 9 of 10

"Nasser M. Abbasi" <nma@12000.org> wrote in message <jc061o$7r9$1@speranza.aioe.org>...
> On 12/10/2011 9:41 AM, Vihang Patil wrote:
> > dpb<none@non.net> wrote in message<jbvrag$ci7$1@speranza.aioe.org>...
> >> On 12/10/2011 8:16 AM, dpb wrote:
> >>> On 12/10/2011 7:32 AM, Vihang Patil wrote:
> >>>> Hello,
> >>>> I have an 10 x m matrix. In this matrix, I sometimes get an entire
> >>>> column of 0. I want to remove this 0 from the matrix...
> >>> ....
> >>>> I want to only remove the zeros that are present in the entire column.
> >>>> If there is 0 in a column but other non zero real numbers are there in
> >>>> that particular column, then I wish to keep the 0....
> >>>
> >>> x(all(x),:)=[];
> >>
> >> Dang, thought I stopped this one...it's missing the negation operator...
> >>
> >> --
> >
> > I tried your code, but I get the following error
> >
> > x(all(x),:)=[];
> > ??? Index of element to remove exceeds matrix dimensions.
> >
> > x(~all(x),:)=[];
> > ??? Index of element to remove exceeds matrix dimensions.
> >
> > Vihang
>
> I do not know what you typed there. Better to show all
> the code you used.
>
> But any way, this works on my system 2011a:
>
> EDU>> A=[1 2 0;2 3 0; 4 5 0]
> A =
> 1 2 0
> 2 3 0
> 4 5 0
>
> EDU>> A(:,~all(A,1))=[]
> A =
> 1 2
> 2 3
> 4 5
> EDU>>
>
> --Nasser

Dear Nasser
If I try this I dont get the matrix I require, if A is the following
A=[1 2 0 0 0;2 3 0 0 1; 4 5 0 0 0;]
In the above case if I try
A(:,~all(A,1))=[]
then I get

A =

     1 2
     2 3
     4 5

Whereas I want;

A =

     1 2 0
     2 3 1
     4 5 0

Vihang

Subject: remove 0's

From: dpb

Date: 16 Dec, 2011 15:10:01

Message: 10 of 10

On 12/16/2011 8:01 AM, Vihang Patil wrote:
> "Nasser M. Abbasi" <nma@12000.org> wrote in message
> <jc061o$7r9$1@speranza.aioe.org>...
>> On 12/10/2011 9:41 AM, Vihang Patil wrote:
...

...

> A=[1 2 0 0 0;2 3 0 0 1; 4 5 0 0 0;]
> In the above case if I try
> A(:,~all(A,1))=[]
> then I get
>
> A =
>
> 1 2
> 2 3
> 4 5
>
> Whereas I want;
>
> A =
>
> 1 2 0
> 2 3 1
> 4 5 0

doc any % you've changed the target, it appears...

--

Tags for this Thread

No tags are associated with 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