Find elements from A in B and get the index of found element in B

62 views (last 30 days)
Hi
I have two arrays, A and B, A has 12,000 elements and B has 260,000 elements.
I need to find every instance when an element in A equals B and get the index of B when this occurs.
So for example at A(5) I need to search all elements of B for the value in A(5) and return the index location of B where it is found.
var_found = []
for i : length(A)
B( find(A(i)) ) = var_found(i);
end
Please can someone advise

Accepted Answer

Jos (10584)
Jos (10584) on 19 Dec 2017
Edited: Jos (10584) on 19 Dec 2017
You want to find all the indices of all elements of A in B? So, some elements of A might occur once, other multiple times, and some not at all. Therefore you need to resort to cell arrays. With arrayfun you can loop over all elements of A:
A = [1 2 3 4]
B = [2 2 1 4 2 1]
C = arrayfun(@(x) find(B==x), A, 'un', 0)
% C{k} now holds the indices into B, where B equals A(k)
  6 Comments
Jos (10584)
Jos (10584) on 19 Dec 2017
You write: For every value contained in array A, find the equivalent value in array B but also get the index position of that value in B.
  • The equivalent value in B will be the same value of A, by definition.
  • The index in B will be the second (or third) output of intersect.
Jos (10584)
Jos (10584) on 19 Dec 2017
btw, did you take floating point problems into account? Two floating points may look the same but may in fact differ minutely, so they are treated as not being equal:
a = 0.3 % looks like 0.3000
b = 0.1 + 0.2 % looks like 0.3000
a==b % false ...
fprintf('%.20f\n',[a b]) % ... as they do differ!
I do begin to suspect your problems are arising from this ...

Sign in to comment.

More Answers (4)

Harish Ramachandran
Harish Ramachandran on 19 Dec 2017
You can use the 'intersect' command in order to extract the common values along with the indices from base arrays A and B.
[output_array,index_a,index_b] = intersect(A,B,'stable');
  2 Comments
Nathan Kennedy
Nathan Kennedy on 19 Dec 2017
Edited: Nathan Kennedy on 19 Dec 2017
Hi, Thanks for the reply.
So I have implemented this and it only returns one set of values, the output is
output array = 0
index_a = 1
index_b = 200001
Which is right, A(1) had its first occurrence at B(200001). But it doesn't find them all... i.e. A(2), A(3)... A(length(A))
Can intersect be looped? It does not show that on the link you gave
Harish Ramachandran
Harish Ramachandran on 19 Dec 2017
Edited: Harish Ramachandran on 19 Dec 2017
Intersect returns all data elements common to A and B. There is no need to loop. If output_array has only one element, it implies that only one common element exists between A and B. However, this method neglects indices in data containing repetitive elements and is suitable for a unique set.
>> A = [ 1.01 2.02 3.03 4.04 5.05 6.06];
>> B = [ 1 2 3.03 4 5.05 6 ];
>> [output_array,index_a,index_b] = intersect(A,B,'stable');
>> output_array
output_array =
3.0300 5.0500
>> index_a
index_a =
3
5
>> index_b
index_b =
3
5

Sign in to comment.


Jan
Jan on 19 Dec 2017
Edited: Jan on 19 Dec 2017
[iA, locB] = ismembertol(A, B, 1e-6);
Or maybe
[iB, locA] = ismembertol(B, A, 1e-6);
If this can be "looped" depends on what "looped" means.

Nathan Kennedy
Nathan Kennedy on 19 Dec 2017
Edited: Nathan Kennedy on 19 Dec 2017
I am interested in what Jo says, but I don't think that's the issue? I have tried what others have written above and it does not seem to be quite working.
So I have placed a section of code below to show its not working.
%Setup Sampling of waveform
f = (20.2 : 0.01 : 21.2)*10^9;
Fs = 3*max(f); %over 2 sampling frequency to prevent nyquist
Ts = 1/Fs;
end_t = 0.2*10^(-6);
number_samples = end_t /Ts;
dt = 0 : Ts : end_t-Ts;
%Generate a lot of sinusoids that will be added to form waveform
for a = 1:length(f)-1
y(a,:) = 0.1 * sin(2*pi .* f(a) .* dt) ;
end
waveform = sum(y); % What we all called Array A
%In Decibels
power_in = [-20 : 1 : 6]';
power_in = [-20 : 0.0001 : 6]'; %interpolated
%In Volts
power_in = 10.^((power_in/20)); %What we all called Array B
%Code advised
[output_array, index_a,index_b] = intersect(abs(waveform),power_in);
  2 Comments
Jos (10584)
Jos (10584) on 19 Dec 2017
I'm 99.99999999...% sure that floating points is one of the issues here when using intersect.
Also did you plot waveform en power_in. waveform contains negative values, whereas power_in does not!
Stephen23
Stephen23 on 20 Dec 2017
"I am interested in what Jo says, but I don't think that's the issue?"
Actually the floating point error is the exact cause of what you are experiencing. Why do you think it isn't?

Sign in to comment.


Jos (10584)
Jos (10584) on 19 Dec 2017
I think you'll find my function NEARESTPOINT pretty helpful in this respect. You can download it from the File Exchange:
Based on your code above:
IDX = nearestpoint(waveform, power_in)
will return an index into B for each value of A, so that the difference between A(k) and B(IDX(k)) is minimal. Nearestpoint is quite fast :)
  3 Comments
Stephen23
Stephen23 on 20 Dec 2017
"Is there another way around this without using nearest point?"
Of course! Did you read Jan Simon's answer? You could also use bsxfun and abs, then compare against a tolerance. You are not limited using nearestpoint, although it does provide a very simple calling syntax.
Jos (10584)
Jos (10584) on 20 Dec 2017
Indeed, of course you can! There are many ways to Rome ...
However, nearestpoint is quite optimised in both speed and memory efficiency. Note that bsxfun creates an intermediate matrix of M-by-N for two vectors with M and N elements.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!