Search an element in another matrix.

조회 수: 4 (최근 30일)
Davin
Davin 2014년 8월 27일
답변: Michael Haderlein 2014년 8월 27일
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