Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
lookup table

Subject: lookup table

From: Petra

Date: 6 Dec, 2012 07:32:13

Message: 1 of 7

Hi all,
I need to find the fastest way to compare to vectors. here is a little example:
% I got 2 vectors
a=[1 2.4 3.1 4.9 5.7]
b=[1.2 5.9 4.7 2.3 9.5 6 4.8] % b is a very long vector
every number in b should search in a for the next lesser number. The answer should be like this:
[1 5.7 3.1 1 5.7 5.7 3.1]

Is there a fast way. Maybe without a loop.
Thank you

Subject: lookup table

From: Nasser M. Abbasi

Date: 6 Dec, 2012 08:13:24

Message: 2 of 7

On 12/6/2012 1:32 AM, Petra wrote:
> a=[1 2.4 3.1 4.9 5.7]
> b=[1.2 5.9 4.7 2.3 9.5 6 4.8] % b is a very long vector
> every number in b should search in a for the next lesser number. The
>answer should be like this:
> [1 5.7 3.1 1 5.7 5.7 3.1]

Can;t answer the 'fastest' part. You can do measurments.

But assuming 'a' is sorted from small to large (you can sort
it first to make sure) then one way:

--------------------------
B=bsxfun(@gt,b,a');
a(arrayfun(@(i) find(B(:,i)==1,1,'last'),1:length(b)))
-------------------------

     1.0000 5.7000 3.1000 1.0000 5.7000 5.7000 3.1000

--Nasser

Subject: lookup table

From: Nasser M. Abbasi

Date: 6 Dec, 2012 08:22:31

Message: 3 of 7

On 12/6/2012 2:13 AM, Nasser M. Abbasi wrote:
> On 12/6/2012 1:32 AM, Petra wrote:
>> a=[1 2.4 3.1 4.9 5.7]
>> b=[1.2 5.9 4.7 2.3 9.5 6 4.8] % b is a very long vector
>> every number in b should search in a for the next lesser number. The
>> answer should be like this:
>> [1 5.7 3.1 1 5.7 5.7 3.1]
>
> Can;t answer the 'fastest' part. You can do measurments.
>
> But assuming 'a' is sorted from small to large (you can sort
> it first to make sure) then one way:
>
> --------------------------
> B=bsxfun(@gt,b,a');
> a(arrayfun(@(i) find(B(:,i)==1,1,'last'),1:length(b)))
> -------------------------
>
> 1.0000 5.7000 3.1000 1.0000 5.7000 5.7000 3.1000
>

I just noticed the above will fail if 'b' has values less
than any in 'a'. To fix this, simply remove those results
first, like this: (not too pretty, should be a better way
but have to go now)

a=[1 2.4 3.1 4.9 5.7]
b=[.5 1.2 5.9 4.7 2.3 9.5 6 4.8] % noticed, add .5 to 'b'

-----------------------------
B = bsxfun(@gt,b,a');
I = arrayfun(@(i) find(B(:,i)==1,1,'last'),1:length(b),'UniformOutput',false);
a(cell2mat(I(cellfun(@length,I)>0)))
-------------------------

1.0000 5.7000 3.1000 1.0000 5.7000 5.7000 3.1000

--Nasser

Subject: lookup table

From: Bruno Luong

Date: 6 Dec, 2012 10:53:07

Message: 4 of 7

"Petra" wrote in message <k9phls$3e1$1@newscl01ah.mathworks.com>...
> Hi all,
> I need to find the fastest way to compare to vectors. here is a little example:
> % I got 2 vectors
> a=[1 2.4 3.1 4.9 5.7]
> b=[1.2 5.9 4.7 2.3 9.5 6 4.8] % b is a very long vector
> every number in b should search in a for the next lesser number. The answer should be like this:
> [1 5.7 3.1 1 5.7 5.7 3.1]

[~, loc] = histc(b,[a inf]);
a(loc)

ARRAYFUN IS (or worse than) a LOOP.

Bruno

Subject: lookup table

From: Nasser M. Abbasi

Date: 6 Dec, 2012 12:37:05

Message: 5 of 7

On 12/6/2012 4:53 AM, Bruno Luong wrote:
> "Petra" wrote in message <k9phls$3e1$1@newscl01ah.mathworks.com>...
>> Hi all,
>> I need to find the fastest way to compare to vectors. here is a little example:
>> % I got 2 vectors
>> a=[1 2.4 3.1 4.9 5.7]
>> b=[1.2 5.9 4.7 2.3 9.5 6 4.8] % b is a very long vector
>> every number in b should search in a for the next lesser number. The answer should be like this:
>> [1 5.7 3.1 1 5.7 5.7 3.1]
>
> [~, loc] = histc(b,[a inf]);
> a(loc)
>

Bruno,

fyi,

The above does not work if 'b' has an element which
happened to be less than any in 'a'

a=[1 2.4 3.1 4.9 5.7]
b=[.5 1.2 5.9 4.7 2.3 9.5 6 4.8]

[~, loc] = histc(b,[a inf]);
a(loc)

>Subscript indices must either be real positive integers or logicals.

loc =

      0 1 5 3 1 5 5 3

--Nasser

Subject: lookup table

From: Bruno Luong

Date: 6 Dec, 2012 13:23:08

Message: 6 of 7

"Nasser M. Abbasi" wrote in message <k9q3hj$mdl$1@speranza.aioe.org>...

> The above does not work if 'b' has an element which
> happened to be less than any in 'a'

This case is not in the OP requirement:

> every number in b should search in a for the next lesser number

One could add -Inf on the left size of 'a' in hist command if needed.

Bruno

Subject: lookup table

From: Petra

Date: 6 Dec, 2012 15:12:06

Message: 7 of 7

I really have to say thank you. sorry about replying so late, i had to take a nap.
I will work with your ideas and hopefully finding the best way for me

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us