Search an element in another matrix.

5 views (last 30 days)
Hello,
I am a newbie and thanks in advance for the help.
I have 2 matrix : one say A with size 217 * 2 and another one B with 10864 * 2 .
Both of theses arrays consists of a date column ( first one) and another column of data.
I need to find the common dates between these 2 matrix.
I have done an intersect between the 2 first columns and i got the common dates.
Now I need to take each of these dates and search in the first and the second matrix and offset it by one column to get the value of the second column on the common dates. In final, I will need to get a matrix with common dates, and 2 other columns corresponding to their values at the common dates.
I tried with a loop with find command, but i am getting an size mismatch which is normal, I also tried to reshape and normalize both arrays but its not working.
I did :
c = intersect(A(:,1),B(:,1) --- Common Dates.
It gave me a matrix of 217 * 2 .
But then I am getting difficulties to search these values in the other 2 matrix.
Thanks for your help.
D

Accepted Answer

Iain
Iain on 27 Aug 2014
[c ia ib] = intersect(A(:,1),B(:,1));
c should be 217 x 1, as should ia & ib.
A(ia,:)
that should spit out the date & the value for all of the intersecting dates in A.
B(ib,:)
that should spit out the date & the value for all of the intersecting dates in B.

More Answers (1)

Michael Haderlein
Michael Haderlein on 27 Aug 2014
intersect has more output arguments:
[C,ia,ib] = intersect(A,B)
So you can use ia and ib to get the values you want:
a=[(1:10)',(1:10)'.^2];
b=[(1:.5:5)',(1:.5:5)'+2];
[C,ia,ib] = intersect(a(:,1),b(:,1));
a(ia,2)
ans =
1
4
9
16
25
>> b(ib,2)
ans =
3
4
5
6
7

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!