help-octave
[Top][All Lists]

## Re: Vectorizing simple loops

 From: pawelz Subject: Re: Vectorizing simple loops Date: Wed, 2 Dec 2015 04:24:00 -0800 (PST)

```Thanks Nick. I had a similar thought too, so actually I posted a bit more
simplified version of this question to the StackExchange half an hour
ago. There
http://stackoverflow.com/questions/34041614/trivial-impossible-algorithm-challenge-in-octave-matlab

On Wed, Dec 2, 2015 at 1:36 PM NJank [via Octave] <

>
> On Dec 2, 2015 6:56 AM, "pawelz" <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=4673756&i=0>> wrote:
> >
> > Hi,
> >
> > I have been profiling some code going through big datasets and find it
> > impossible to express these simple algorithms in high-performing
> vectorized
> > form instead of slow loop version which is so trivial I don't even show
> it
> > this point is to re-write these in C instead, which i would like to
> avoid.
> >
> > Case A) Fill the blanks with last non-zero value:
> > Go through the input vector values one by one and output the value if not
> > zero, or copy over the last non-zero value. Sample input and expected
> output
> > vectors:
> > in  = [ 1 0 2 0 7 7 7 0 5 0 0 0 9 ]
> > out = [ 1 1 2 2 7 7 7 7 5 5 5 5 9 ]
> > I tried the merge(v==0, shift()...) but it only works for the first zero
> > occurrence, not an arbitrary number of them.
> >
> > Case B) seems a bit more difficult but in fact it is simple. Produce an
> > output with a rule based on simple memory of the previous step. The basic
> > trick would be to make a decision in each step, based on the previous
> steps
> > output, i.e. produce out(i) based on the value of acc which was built
> using
> > out(i-1). Again tried merge(shift()) to no avail...
> >
> > in  = [2 2 1 -1 0 0 -2 2 0 1]
> > x   = 1;
> > top = 5;
> > acc = 0;
> > for i = 1 : length(in)
> >   if     (in(i) == +2)
> >           if (acc <      0) out(i) = -acc+x;
> >       elseif (acc >  top-x) out(i) = 0;
> >       elseif (acc >=     0) out(i) = x;
> >        endif
> >   elseif (in(i) == +1)
> >           if (acc >=     0) out(i) = 0;
> >       elseif (acc <      0) out(i) = -acc;
> >        endif
> >   elseif (in(i) ==       0) out(i) = 0;
> >   elseif (in(i) ==      -1)
> >           if (acc <=     0) out(i) = 0;
> >       elseif (acc >      0) out(i) = -acc;
> >        endif
> >   elseif (in(i) == -2)
> >           if (acc >      0) out(i) = -acc-x;
> >       elseif (acc < -top+x) out(i) = 0;
> >       elseif (acc <=     0) out(i) = -x;
> >        endif
> >   endif
> >   acc += out(i);
> > endfor
> > out
> >
> > Cheers
> > Pawel
> >
>
> I'll take a look later today, but in case you don't get a solution here,
> the people over at StackExchange watching the Matlab/Octave tags (some of
> whom are here) are really good at popping out code snippets for things like
> this.
>
> Nick J
>
> _______________________________________________
> Help-octave mailing list
> [hidden email] <http:///user/SendEmail.jtp?type=node&node=4673756&i=1>
> https://lists.gnu.org/mailman/listinfo/help-octave
>
>
> ------------------------------
> below:
>
> http://octave.1599824.n4.nabble.com/Vectorizing-simple-loops-tp4673742p4673756.html
> <http://octave.1599824.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4673742&code=cGF3ZWwuemFsdXNraUBnbWFpbC5jb218NDY3Mzc0MnwxMzI1ODQ5NTc5>
> .
> NAML
>

--
View this message in context:
http://octave.1599824.n4.nabble.com/Vectorizing-simple-loops-tp4673742p4673757.html
Sent from the Octave - General mailing list archive at Nabble.com.

```