Find elements from A in B and get the index of found element in B
53 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Jos (10584)
2017-12-19
编辑:Jos (10584)
2017-12-19
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 个评论
Jos (10584)
2017-12-19
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)
2017-12-19
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 ...
更多回答(4 个)
Harish Ramachandran
2017-12-19
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 个评论
Harish Ramachandran
2017-12-19
编辑:Harish Ramachandran
2017-12-19
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
Nathan Kennedy
2017-12-19
编辑:Nathan Kennedy
2017-12-19
2 个评论
Jos (10584)
2017-12-19
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
2017-12-20
"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?
Jos (10584)
2017-12-19
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 个评论
Stephen23
2017-12-20
"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)
2017-12-20
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!