[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Vectorize this loop?
From: |
Jaroslav Hajek |
Subject: |
Re: Vectorize this loop? |
Date: |
Fri, 24 Oct 2008 10:29:47 +0200 |
On Fri, Oct 24, 2008 at 9:42 AM, Matthias Brennwald
<address@hidden> wrote:
> Dear Octavers
>
> Is it possible to get rid of the for loop in the below code by replacing
> it by some vectorized code? I somehow think it is, but I can't see how.
> Any ideas would be appreciated.
>
> Thanks
> Matthias
>
>
> --------------------
> point_count = round (rand(1000,1))*5 + 20; % this is just a dummy to
> illustrate the problem.
> N_vals = sum (point_count);
> N_scans = length (point_count);
>
> scan_idx = repmat (NaN,N_vals,1);
> ind = cumsum (point_count); ind = [0 ; ind];
> for ii = 1:N_scans
> scan_idx (ind(ii)+1:ind(ii+1)) = ii;
> end
> --------------------
>
For example, replace the for loop by:
scan_idx = lookup (ind, 0:N_vals-1)';
cheers,
--
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz