vectorization
David Bateman
David.Bateman at motorola.com
Wed Jul 16 17:33:22 CDT 2008
Josh Bell wrote:
> Hi, this is my first time trying to ask a question here, so i hope this makes sense. What I am trying to do involves vectorizing an M-file that I wrote. I have a long data file, which is basically just a string of numbers. It is meant to represent a certain signal embedded in noise. What I want to do is take a certain piece of that data file (say, the first 10 data points, for instance) and replace all of the data points within that window with the value that is nearest to the mean of that window. In other words, I would take these 10 points from the input (which is an array with 1 column and approximately 19000 rows), and place them in a separate array (the output array). I would then find the value within the output array that was closest to the mean of that chunk of data, and replace all 10 values with this number. I would then do the same with points 21-30, 31-40, etc. I have already successfully done this with a 'for' loop, but it runs slower than cold molasses!
> . I will include that code below.
>
> function q = newfilter1 (A, npoints)
>
> %A is the input array
>
> for n = npoints+1:length(A)-npoints
>
> a = A(n-npoints:n+npoints);
>
> b(1,1:length(a)) = a';
>
> b(2,1:length(a)) = mean(a);
>
> [avg, Ia] = min(abs(diff(b)));
>
> q(n-npoints:n+npoints) = b(1,Ia);
>
> end
> endfunction
>
> any help would be greatly appreciated. Thanks!
>
Won't
function q = newfilter1 (A, npoints)
% assumes A is a muliple of npoints long
a = reshape (A, npoints, numel (A) / npoints);
q = reshape (repmat (min (abs (A - repmat(mean (A, 1), npoints, 1)),
1), npoints, 1), size (A));
endfunction
do what you want?
D.
> _________________________________________________________________
> Time for vacation? WIN what you need- enter now!
> http://www.gowindowslive.com/summergiveaway/?ocid=tag_jlyhm
> _______________________________________________
> Help-octave mailing list
> Help-octave at octave.org
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
>
More information about the Help-octave
mailing list