[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
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
have already been some reactions.
http://stackoverflow.com/questions/34041614/trivial-impossible-algorithm-challenge-in-octave-matlab
On Wed, Dec 2, 2015 at 1:36 PM NJank [via Octave] <
address@hidden> wrote:
>
> 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
> > here in the first case. I would appreciate your advice. My only answer at
> > 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
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://octave.1599824.n4.nabble.com/Vectorizing-simple-loops-tp4673742p4673756.html
> To unsubscribe from Vectorizing simple loops, click here
> <http://octave.1599824.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4673742&code=cGF3ZWwuemFsdXNraUBnbWFpbC5jb218NDY3Mzc0MnwxMzI1ODQ5NTc5>
> .
> NAML
> <http://octave.1599824.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.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.
- Vectorizing simple loops, pawelz, 2015/12/02
- Re: Vectorizing simple loops, Nicholas Jankowski, 2015/12/02
- Re: Vectorizing simple loops,
pawelz <=
- Re: Vectorizing simple loops, Brian Kaczynski, 2015/12/02
- Re: Vectorizing simple loops, Francesco PotortÃ¬, 2015/12/02
- Re: Vectorizing simple loops, pawelz, 2015/12/02
- Re: Vectorizing simple loops, Brian Kaczynski, 2015/12/02
- Re: Vectorizing simple loops, pawelz, 2015/12/03
- Re: Vectorizing simple loops, Francesco PotortÃ¬, 2015/12/03
- RE: Vectorizing simple loops, Jose Marcos Ferraro, 2015/12/03
- RE: Vectorizing simple loops, pawelz, 2015/12/06
- RE: Vectorizing simple loops, Jose Marcos Ferraro, 2015/12/07