From: Nate Kando on
Given a number and a vector of number data,
if I would like to, as quickly and resource conservant as possible,
find which number within the vector, or more importantly,
which nth term it is,
is there a specific command for this purpose?

For example,
a = 5.18,
b = [1 2 3 -4 5 0 5.1 5.2 5.3 5.5 6 7 8 9 10]

I'd like to know that
a is closest to b(8)

Currently,
I check the absolute value difference between the two
and then take the minimum value of that new matrix like this:

dif = abs(b-a);
[~, difmin_i] = min(dif);

however, i was told this is resource heavy
for a dynamic that will check this very often
and that there must be a more efficient method.
From: us on
"Nate Kando" <nate.kando.nothis(a)gmail.com> wrote in message <hnqm7v$hn3$1(a)fred.mathworks.com>...
> Given a number and a vector of number data,
> if I would like to, as quickly and resource conservant as possible,
> find which number within the vector, or more importantly,
> which nth term it is,
> is there a specific command for this purpose?
>
> For example,
> a = 5.18,
> b = [1 2 3 -4 5 0 5.1 5.2 5.3 5.5 6 7 8 9 10]
>
> I'd like to know that
> a is closest to b(8)
>
> Currently,
> I check the absolute value difference between the two
> and then take the minimum value of that new matrix like this:
>
> dif = abs(b-a);
> [~, difmin_i] = min(dif);
>
> however, i was told this is resource heavy
> for a dynamic that will check this very often
> and that there must be a more efficient method.

one of the many solutions
- this allows you to compare more than one A in one shot...

b=[1 2 3 -4 5 0 5.1 5.2 5.3 5.5 6 7 8 9 10];
a=[5.18, 5.17, 4,6.1,7];
bu=unique(b);
[n,ix]=histc(a,bu);
r=bu(ix);
disp([a;r]);
%{
5.18 5.17 4 6.1 7 % <- a
5.1 5.1 3 6 7 % <- r
%}

us
From: Jan Simon on
Dear Nate!

> Given a number and a vector of number data,
> if I would like to, as quickly and resource conservant as possible,
> find which number within the vector, or more importantly,
> which nth term it is,
> is there a specific command for this purpose?
>
> For example,
> a = 5.18,
> b = [1 2 3 -4 5 0 5.1 5.2 5.3 5.5 6 7 8 9 10]
>
> I'd like to know that
> a is closest to b(8)

As long as b is not sorted, you have to compare all values in general. Is it possible to create b in sorted order? Then a bsearch method as C-mex would be much faster.

Kind regards, Jan
From: Nate Kando on
Much thanks for the help and the tags, 'us' and Jan.

Also, for Jan,
I asked for the more general case for future use,
but in this case, b is in fact sorted
(it is a cubic function that starts from 0, and therefore continually ascends)
That'd be worth researching C-mex?

I'll definitely look into both methods;
much appreciated.
 | 
Pages: 1
Prev: signal
Next: video compression matlab code