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:
Speeding up removal of zero columns

Subject: Speeding up removal of zero columns

From: Kurt

Date: 29 Aug, 2010 19:36:04

Message: 1 of 5

Hi,

I've got the following line of code to remove columns containing only zeros:
routes2=routes2(:,any(routes2));

According to the profiler this line of code is good for 10% of my computational time. Is there any way to rewrite it or to use other statements so that this column removal process can be sped up?

Thanks,
Kurt

Subject: Speeding up removal of zero columns

From: Walter Roberson

Date: 29 Aug, 2010 20:20:53

Message: 2 of 5

On 29/08/10 2:36 PM, Kurt wrote:

> I've got the following line of code to remove columns containing only
> zeros:
> routes2=routes2(:,any(routes2));
>
> According to the profiler this line of code is good for 10% of my
> computational time. Is there any way to rewrite it or to use other
> statements so that this column removal process can be sped up?

That approach should be pretty efficient. Possibly you might be able to
do better by mex'ing up a function that took into account cache effects
and memory manager properties; the fastest implementation would probably
want to take into account your prior knowledge about likely sizes of the
vectors and about the relative portion to be retained. For example,
sometimes it makes sense to deliberately use more rows than you need so
that each column is well-aligned to be DMA'd to the output matrix, which
is a time vs memory tradeoff whose suitability would depend upon the
sizes you are working with.

Subject: Speeding up removal of zero columns

From: Roger Stafford

Date: 29 Aug, 2010 20:40:06

Message: 3 of 5

"Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message <i5ecr3$bof$1@fred.mathworks.com>...
> Hi,
>
> I've got the following line of code to remove columns containing only zeros:
> routes2=routes2(:,any(routes2));
>
> According to the profiler this line of code is good for 10% of my computational time. Is there any way to rewrite it or to use other statements so that this column removal process can be sped up?
>
> Thanks,
> Kurt
- - - - - - - - - -
  As your code is now, every element of every column must be tested. If your matrix contains many nonzero columns and you have long columns, you might possibly speed things up with a for-loop using the find instruction. For each column do a find of that column using the second 'k' argument of 1 so that it (hopefully) stops after the first nonzero, and set the corresponding element of a logical vector according to whether the find result is non-empty or not. Then use that logical vector to eliminate the all-zero columns.

 t = false(1,size(x,2));
 for k = 1:size(x,2)
  t(k) = ~isempty(find(x(:,k),1));
 end
 x = x(:,t);

Roger Stafford

Subject: Speeding up removal of zero columns

From: Walter Roberson

Date: 29 Aug, 2010 21:33:14

Message: 4 of 5

On 29/08/10 3:40 PM, Roger Stafford wrote:
> "Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message
> <i5ecr3$bof$1@fred.mathworks.com>...

>> routes2=routes2(:,any(routes2));

> As your code is now, every element of every column must be tested.

No, any() and all both short-circuit; this can be demonstrated with some
simple timing tests on larger arrays.

Subject: Speeding up removal of zero columns

From: Bruno Luong

Date: 29 Aug, 2010 21:49:04

Message: 5 of 5

"Kurt " <jef_kabouter_wesley@hotmail.com> wrote in message <i5ecr3$bof$1@fred.mathworks.com>...
> Hi,
>
> I've got the following line of code to remove columns containing only zeros:
> routes2=routes2(:,any(routes2));
>
> According to the profiler this line of code is good for 10% of my computational time. Is there any way to rewrite it or to use other statements so that this column removal process can be sped up?

Sorry, but there is little hope. Removal/concatenation is expensive and should be avoid when it's possible.

Bruno

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