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:
vectorization suggestion

Subject: vectorization suggestion

From: Piero Lanucara

Date: 16 Jul, 2011 22:04:08

Message: 1 of 5

Hi to all,
There exist a simple and fast way to do this:

v_orig=[1 0 2 0 3 0 4 0 0 5 0 0 6] in
v_end=[1 2 2 3 3 4 4 5 5 5 6 6 6]

in the end the 0 values has to be replace with the first non zero element shifting towards the right direction....of course this vector is only an example because I use very huge vector like this!
thank's in advance
Piero

Subject: vectorization suggestion

From: Roger Stafford

Date: 17 Jul, 2011 00:13:08

Message: 2 of 5

"Piero Lanucara" <lanucara@caspur.it> wrote in message <ivt1so$1uu$1@newscl01ah.mathworks.com>...
> Hi to all,
> There exist a simple and fast way to do this:
>
> v_orig=[1 0 2 0 3 0 4 0 0 5 0 0 6] in
> v_end=[1 2 2 3 3 4 4 5 5 5 6 6 6]
>
> in the end the 0 values has to be replace with the first non zero element shifting towards the right direction....of course this vector is only an example because I use very huge vector like this!
> thank's in advance
> Piero
- - - - - - - - - -
  What's wrong with just using a simple for-loop? It may be about as efficient as any vectorization and is a lot easier to code.

 v_end = v_orig;
 t = 0;
 for k = length(v_end):-1:1
  if v_end(k) == 0, v_end(k) = t; else, t = v_end(k); end
 end

Roger Stafford

Subject: vectorization suggestion

From: Matt J

Date: 17 Jul, 2011 00:40:13

Message: 3 of 5

"Roger Stafford" wrote in message <ivt9ek$jq3$1@newscl01ah.mathworks.com>...
>
> What's wrong with just using a simple for-loop? It may be about as efficient as any vectorization and is a lot easier to code.
>
=========================

I agree that this might be a case of vectorization overkill. Nevertheless, the following might be a better alternative to a for-loop if the strings of zeros are all fairly short

v_end=v_orig;
idx=1;
while any(idx)
   
   s = circshift(v_end,[0,-1]);
   idx=s & ~v_end;
   v_end(idx)=s(idx);
   
end

Subject: vectorization suggestion

From: Piero Lanucara

Date: 17 Jul, 2011 07:44:08

Message: 4 of 5

many thank's for the suggestions
I agree that probably the first implentation is faster with huge (the dimension is more or less the half number of non zeros of large symmetric sparse) matrix
probably there's some chance to do calculation in place so some benefit should occurr
thanks!

P.



"Matt J" wrote in message <ivtb1d$ng6$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <ivt9ek$jq3$1@newscl01ah.mathworks.com>...
> >
> > What's wrong with just using a simple for-loop? It may be about as efficient as any vectorization and is a lot easier to code.
> >
> =========================
>
> I agree that this might be a case of vectorization overkill. Nevertheless, the following might be a better alternative to a for-loop if the strings of zeros are all fairly short
>
> v_end=v_orig;
> idx=1;
> while any(idx)
>
> s = circshift(v_end,[0,-1]);
> idx=s & ~v_end;
> v_end(idx)=s(idx);
>
> end

Subject: vectorization suggestion

From: Bruno Luong

Date: 18 Jul, 2011 06:23:08

Message: 5 of 5

v_orig=[0 1 0 2 0 3 0 4 0 0 5 0 0 6]
 
v = fliplr(v_orig);
idx = v~=0;
d = diff(v(idx));
idx(find(idx,1,'first')) = false;
v(idx) = d;
v = fliplr(cumsum(v))

% 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