Am 19.10.2017 um 14:50 schrieb Artur T.:
> I am facing the following simple problem
Are you sure you don't simply need the built-in resample() function?
> for which I only find a loop solution but loops are (generally?) slow
> compared to vectorized approaches (if possible).
Actually, in my experience loops aren't so slow in hansl. There have
been cases where I invested some effort to replace a loop with some
matrix-oriented tricks and didn't get any speed gains. But of course
it depends on the particular case.
> In MATLAB this works simply by: X = Y(SEL)
Have you tried Y[SEL] (square brackets, as with all indexation in hansl)?
cheers,
sven
Oh my god, this was so trivial. Thank you, Sven! I totally forgot about
this solution... :-/ In the example below the gain in speed is enormous
(26x faster)
<hansl>
set verbose off
scalar T = 100
matrix Y = seq(1,T)' # original realizations
set stopwatch
loop j=1..1000 -q
SEL = ceil(muniform(T,1)*10) # selection vector (select i-th
row from Y)
matrix X = zeros(T,1) # put re-sampled Y values here
loop i=1..rows(SEL) -q
X[i]=Y[SEL[i]] # i-th position Y value
endloop
endloop
scalar T1 = $stopwatch
printf "Loop version = %.3f sec.\n", $stopwatch
set stopwatch
loop j=1..1000 -q
SEL = ceil(muniform(T,1)*10) # selection vector (select i-th
row from Y)
matrix X = zeros(T,1) # put re-sampled Y values here
X = Y[SEL]
endloop
scalar T2 = $stopwatch
printf "Vectorized version = %.3f sec.\n", $stopwatch
eval T1/T2
</hanslY
Artur