Vectorize this loop?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Vectorize this loop?

Matthias Brennwald (bwm)
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
--------------------

_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Vectorize this loop?

Jaroslav Hajek-2
On Fri, Oct 24, 2008 at 9:42 AM, Matthias Brennwald
<[hidden email]> 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
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave