From: dave levine on
I have to find the nearest value from one set of data to another set, without going over. The two sets are two different timesteps.

so for each value of time2, i need to find the nearest-without-going-over corresponding time1 value (its index)

I do it this way right now, and it takes forever, but I get the correct result

time1 = 0:1/44100:1-1/44100;
time2 = 0:1/48000:1-1/48000;
n1(1:size(time2,2)) = 0;

for i = 1:size(time2,2)
for j = 1:size(time1,2)
if time1(j)<=time2(i)
n1(i) = j;
end
end
end

Is there a way to speed this up? My background is not in coding, so I'm having trouble with this.

Thanks!
From: Matthew Whitaker on
"dave levine" <drp2_delete.this(a)unh.edu> wrote in message <hks33n$cj5$1(a)fred.mathworks.com>...
> I have to find the nearest value from one set of data to another set, without going over. The two sets are two different timesteps.
>
> so for each value of time2, i need to find the nearest-without-going-over corresponding time1 value (its index)
>
> I do it this way right now, and it takes forever, but I get the correct result
>
> time1 = 0:1/44100:1-1/44100;
> time2 = 0:1/48000:1-1/48000;
> n1(1:size(time2,2)) = 0;
>
> for i = 1:size(time2,2)
> for j = 1:size(time1,2)
> if time1(j)<=time2(i)
> n1(i) = j;
> end
> end
> end
>
> Is there a way to speed this up? My background is not in coding, so I'm having trouble with this.
>
> Thanks!

I'm sure that there is a better way but this reduces from 74 to 9sec on my system

time1 = 0:1/44100:1-1/44100;
time2 = 0:1/48000:1-1/48000;
n2 = zeros(size(time2));

for i = 1:size(time2,2)
n2(i) = find(time1 <= time2(i),1,'last');
end


% I checked that n1 from your loop and n2 from this one are equal

Hope this helps
Matt W
From: Oleg Komarov on
"dave levine"
> I have to find the nearest value from one set of data to another set, without going over. The two sets are two different timesteps.
>
> so for each value of time2, i need to find the nearest-without-going-over corresponding time1 value (its index)
>
> I do it this way right now, and it takes forever, but I get the correct result
>
> time1 = 0:1/44100:1-1/44100;
> time2 = 0:1/48000:1-1/48000;
> n1(1:size(time2,2)) = 0;
>
> for i = 1:size(time2,2)
> for j = 1:size(time1,2)
> if time1(j)<=time2(i)
> n1(i) = j;
> end
> end
> end
>
> Is there a way to speed this up? My background is not in coding, so I'm having trouble with this.
>
> Thanks!

Help histc

Example:
% evaluation time of your approach: 34.923162 seconds.

% histc approach:
tic
edges = [time1 inf];
[trash,n2] = histc(time2,edges);
toc
Elapsed time is 0.005901 seconds.
% Are they equal
isequal(n1,n2)
ans =
1

Oleg
From: dave levine on
"Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message <hks7nc$74q$1(a)fred.mathworks.com>...
> "dave levine"
> > I have to find the nearest value from one set of data to another set, without going over. The two sets are two different timesteps.
> >
> > so for each value of time2, i need to find the nearest-without-going-over corresponding time1 value (its index)
> >
> > I do it this way right now, and it takes forever, but I get the correct result
> >
> > time1 = 0:1/44100:1-1/44100;
> > time2 = 0:1/48000:1-1/48000;
> > n1(1:size(time2,2)) = 0;
> >
> > for i = 1:size(time2,2)
> > for j = 1:size(time1,2)
> > if time1(j)<=time2(i)
> > n1(i) = j;
> > end
> > end
> > end
> >
> > Is there a way to speed this up? My background is not in coding, so I'm having trouble with this.
> >
> > Thanks!
>
> Help histc
>
> Example:
> % evaluation time of your approach: 34.923162 seconds.
>
> % histc approach:
> tic
> edges = [time1 inf];
> [trash,n2] = histc(time2,edges);
> toc
> Elapsed time is 0.005901 seconds.
> % Are they equal
> isequal(n1,n2)
> ans =
> 1
>
> Oleg


Oleg,
Thank you. Do I just place this inside a single for loop? I need a whole vector of values of n1.

Also, how does this histc function work? I need to be able to translate this code into C++ after I get this matlab algorithm working.

Thanks
From: Oleg Komarov on
"dave levine"
> Oleg,
> Thank you. Do I just place this inside a single for loop? I need a whole vector of values of n1.
>
> Also, how does this histc function work? I need to be able to translate this code into C++ after I get this matlab algorithm working.
>
> Thanks

The power of matlab is that you don't need loops here.
n2 is already a vector of values, same as n1 (see te equal test above).

histc is a built-in matlab fcn, so code isn't available.

Matlab is definitely not the starting point to write in C++ style since its language tries to avoid as much as possible loops.

Oleg