Thread Subject:
how to remove leading zeros in a matrix

Subject: how to remove leading zeros in a matrix

From: Coralee Laubman

Date: 13 Mar, 2011 01:01:05

Message: 1 of 16

I have a matrix
A = [ 1 2 3 4 0 0 0; ...
         1 2 3 4 5 0 0]

and need to reduce it to
 
A = [1 2 3 4 0; ...
        1 2 3 4 5]

for graphing purposes

Subject: how to remove leading zeros in a matrix

From: Nasser M. Abbasi

Date: 13 Mar, 2011 01:10:44

Message: 2 of 16

On 3/12/2011 5:01 PM, Coralee Laubman wrote:
> I have a matrix
> A = [ 1 2 3 4 0 0 0; ...
> 1 2 3 4 5 0 0]
>
> and need to reduce it to
>
> A = [1 2 3 4 0; ...
> 1 2 3 4 5]
>
> for graphing purposes

may be

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

EDU>> [I,J] = find(A==0);
EDU>> A = A(:,1:min(J))
-----------------------

A =
      1 2 3 4 0
      1 2 3 4 5

The above assumes there are NO zeros, other than trailing ones.
If you have zero in the middle of your data, then it wont work.

--Nasser

Subject: how to remove leading zeros in a matrix

From: Nasser M. Abbasi

Date: 13 Mar, 2011 01:14:34

Message: 3 of 16

On 3/12/2011 5:10 PM, Nasser M. Abbasi wrote:

>
> -----------------------------
> EDU>> A = [ 1 2 3 4 0 0 0;
> 1 2 3 4 5 0 0];
>
> EDU>> [I,J] = find(A==0);
> EDU>> A = A(:,1:min(J))
> -----------------------
>
> A =
> 1 2 3 4 0
> 1 2 3 4 5
>
> The above assumes there are NO zeros, other than trailing ones.
> If you have zero in the middle of your data, then it wont work.
>
> --Nasser

Sorry, meant to go for the max, not min, here it is again

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

[I,J] = find(A==0);
A = A(:,1:max(J)-1)
-----------------------
A =
      1 2 3 4 0 0
      1 2 3 4 5 0
      1 2 3 4 5 6


--Nasser

Subject: how to remove leading zeros in a matrix

From: Nasser M. Abbasi

Date: 13 Mar, 2011 01:20:36

Message: 4 of 16

Lets start again :)

---------------------------
A = [ 1 2 3 4 0 0 0 0;
       1 2 3 4 5 0 0 0;
       1 2 3 4 5 6 0 0];
    
[I,J] = find(A~=0)
A = A(:,1:max(J))
-----------------------

A =
      1 2 3 4 0 0
      1 2 3 4 5 0
      1 2 3 4 5 6

I think that is it.

--Nasser

Subject: how to remove leading zeros in a matrix

From: Bruno Luong

Date: 13 Mar, 2011 06:38:04

Message: 5 of 16

One liner solution:

 A(:,find(any(A,1),1,'last')+1:end) = []

Subject: how to remove leading zeros in a matrix

From: molisoft.tm@gmail.com

Date: 4 Jul, 2012 07:25:15

Message: 6 of 16

Suppose we have this matrix:
a = [ 0 0 0 0 0 0 0 0 0 0
      0 0 1 2 3 4 0 0 0 0
      0 0 0 1 2 3 4 0 0 0
      0 0 1 2 3 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 ]

How to produce this:
b = [ 1 2 3 4 0
      0 1 2 3 4
      1 2 3 0 0 ]

Thanks.

Subject: how to remove leading zeros in a matrix

From: molisoft.tm@gmail.com

Date: 4 Jul, 2012 07:44:17

Message: 7 of 16

On Wednesday, July 4, 2012 11:55:15 AM UTC+4:30, molis...@gmail.com wrote:
> Suppose we have this matrix:
> a = [ 0 0 0 0 0 0 0 0 0 0
> 0 0 1 2 3 4 0 0 0 0
> 0 0 0 1 2 3 4 0 0 0
> 0 0 1 2 3 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 ]
>
> How to produce this:
> b = [ 1 2 3 4 0
> 0 1 2 3 4
> 1 2 3 0 0 ]
>
> Thanks.

I used your one liner solution and did it:

d(:,find(any(d,1),1,'last')+1:end) = []
d(:,1:find(any(d,1),1,'first')-1) = []

d(1:find(any(d',1),1,'first')-1,:) = []
d(find(any(d',1),1,'last')+1:end,:) = []

Thanks.

Subject: how to remove leading zeros in a matrix

From: Kristin

Date: 4 Jul, 2012 17:00:08

Message: 8 of 16

Just thought I'd throw in an alternative solution...
Find the rows and columns that add to zero:

cols = sum(a,1);
rows = sum(a,2);
a(:,~cols) = [];
a(~rows,:) = [];

Subject: how to remove leading zeros in a matrix

From: dpb

Date: 4 Jul, 2012 17:16:35

Message: 9 of 16

On 7/4/2012 12:00 PM, Kristin wrote:
> Just thought I'd throw in an alternative solution...
> Find the rows and columns that add to zero:
>
> cols = sum(a,1);
> rows = sum(a,2);
> a(:,~cols) = [];
> a(~rows,:) = [];

I agree 'tis simpler--I didn't follow the need for what appeared overly
complex syntax above--another alternative based on any()

a(:,~any(a,1))=[];
a(~any(a,2),:)=[];

--

Subject: how to remove leading zeros in a matrix

From: james bejon

Date: 4 Jul, 2012 19:25:08

Message: 10 of 16

Since all the sensible solutions have already been taken…

a = [ 0 0 0 0 0 0 0 0 0 0
      0 0 1 2 3 4 0 0 0 0
      0 0 0 1 2 3 4 0 0 0
      0 0 1 2 3 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 ]
[i, j] = find(a);
accumarray([i, j], nonzeros(a), [])

Subject: how to remove leading zeros in a matrix

From: james bejon

Date: 4 Jul, 2012 21:01:07

Message: 11 of 16

...oops, didn't notice trimming the initial rows...

[i, j, s] = find(a);
accumarray([i-min(i)+1, j], s)

Subject: how to remove leading zeros in a matrix

From: Bruno Luong

Date: 5 Jul, 2012 05:20:08

Message: 12 of 16

dpb <none@non.net> wrote in message <jt1tpj$h93$1@speranza.aioe.org>...
> On 7/4/2012 12:00 PM, Kristin wrote:
> > Just thought I'd throw in an alternative solution...
> > Find the rows and columns that add to zero:
> >
> > cols = sum(a,1);
> > rows = sum(a,2);
> > a(:,~cols) = [];
> > a(~rows,:) = [];
>
> I agree 'tis simpler--I didn't follow the need for what appeared overly
> complex syntax above--another alternative based on any()
>
> a(:,~any(a,1))=[];
> a(~any(a,2),:)=[];

Both solutions do remove nil-columns enclosed in between two non-columns one. OP ask for removing of trailing and/or heading blocks of zeros.

Bruno

Subject: how to remove leading zeros in a matrix

From: Jos (10584)

Date: 5 Jul, 2012 06:27:13

Message: 13 of 16

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jt3868$1rk$1@newscl01ah.mathworks.com>...
> dpb <none@non.net> wrote in message <jt1tpj$h93$1@speranza.aioe.org>...
> > On 7/4/2012 12:00 PM, Kristin wrote:
> > > Just thought I'd throw in an alternative solution...
> > > Find the rows and columns that add to zero:
> > >
> > > cols = sum(a,1);
> > > rows = sum(a,2);
> > > a(:,~cols) = [];
> > > a(~rows,:) = [];
> >
> > I agree 'tis simpler--I didn't follow the need for what appeared overly
> > complex syntax above--another alternative based on any()
> >
> > a(:,~any(a,1))=[];
> > a(~any(a,2),:)=[];
>
> Both solutions do remove nil-columns enclosed in between two non-columns one. OP ask for removing of trailing and/or heading blocks of zeros.
>
> Bruno

This will remove all trailing and leading all-zero columns, but keep the ones in-between:

% data
  A = [0 1 0 2 3 0 ; 0 1 0 0 3 0]
% engine
  q = all(A==0)
  c = cumsum(q)
  q = q & (q(1) .* (c==c(1)) | q(end) .* (c == c(end)))
% result
  B = A(:,~q)

~ Jos
http://www.mathworks.nl/matlabcentral/fileexchange/authors/10584

Subject: how to remove leading zeros in a matrix

From: Bruno Luong

Date: 5 Jul, 2012 06:41:07

Message: 14 of 16

molisoft.tm@gmail.com wrote in message <bd9f4c30-7e7f-42dc-84e8-3ff1e92fe284@googlegroups.com>...
> On Wednesday, July 4, 2012 11:55:15 AM UTC+4:30, molis...@gmail.com wrote:
> > Suppose we have this matrix:
> > a = [ 0 0 0 0 0 0 0 0 0 0
> > 0 0 1 2 3 4 0 0 0 0
> > 0 0 0 1 2 3 4 0 0 0
> > 0 0 1 2 3 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 ]
> >
> > How to produce this:
> > b = [ 1 2 3 4 0
> > 0 1 2 3 4
> > 1 2 3 0 0 ]
> >
> > Thanks.
>
> I used your one liner solution and did it:
>
> d(:,find(any(d,1),1,'last')+1:end) = []
> d(:,1:find(any(d,1),1,'first')-1) = []
>
> d(1:find(any(d',1),1,'first')-1,:) = []
> d(find(any(d',1),1,'last')+1:end,:) = []

It is better to use
any(d,2) rather than any(d',1)

Bruno

Subject: how to remove leading zeros in a matrix

From: Bruno Luong

Date: 5 Jul, 2012 06:45:12

Message: 15 of 16

molisoft.tm@gmail.com wrote in message <6b245533-5d82-4d32-b8d8-8618719f1e64@googlegroups.com>...
> Suppose we have this matrix:
> a = [ 0 0 0 0 0 0 0 0 0 0
> 0 0 1 2 3 4 0 0 0 0
> 0 0 0 1 2 3 4 0 0 0
> 0 0 1 2 3 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 ]
>
> How to produce this:
> b = [ 1 2 3 4 0
> 0 1 2 3 4
> 1 2 3 0 0 ]
>

Another way:

[i j]=find(a);
b = a(min(i):max(i),min(j):max(j));

% Bruno

Subject: how to remove leading zeros in a matrix

From: dpb

Date: 5 Jul, 2012 12:48:23

Message: 16 of 16

On 7/5/2012 12:20 AM, Bruno Luong wrote:
...

> Both solutions do remove nil-columns enclosed in between two non-columns
> one. OP ask for removing of trailing and/or heading blocks of zeros.
...

Ah, I overlooked that was really a requirement given there were no such
in the example...

--

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
logical indexing Kristin 4 Jul, 2012 13:04:13
rssFeed for this Thread

Contact us