Search an element in another matrix.

4 次查看(过去 30 天)
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

采纳的回答

Iain
Iain 2014-8-27
[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.

更多回答(1 个)

Michael Haderlein
Michael Haderlein 2014-8-27
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by