Intersect - extraction of row data based on matched results
Show older comments
Hi everyone, new to matlab codes and need help. Please see attached image. I have 2 datasets, Data1 and Data2. I want to compare each date/time in Data2 to the first column of Data1 and if a match if found, extract all the corresponding row data in Data2 for those matched date/time. In the attached image, comparing the date/time in Data2 to Data1, two matches were found, row 1 and 4 in Data1. The centire corresponding row data for those matched date/time in Data1 are extracted into another matrix called Result.
Any hint or code to go about this?
Thank you!

Answers (1)
date1=[44562.00;44563.00;44564.00];
date2=[44562.00;44564.00];
a={'t1';'t2';'t3'};
b=[1;1;1];
c=[2;3;4];
T1=table(a,b,c);
[Lia,Locb]=ismember(date2,date1);
result=T1(Locb,:)
6 Comments
Learning
on 19 Mar 2022
you can use unique function for unique data, for example
date1=[44562.00;44563.00;44564.00;44565.00;44566.00;44562.00;44564.00];
out=unique(date1)
here, 44562.00 and 44564.00 are repeated 2 times. but unique function can handles this.
Learning
on 19 Mar 2022
date1=[44562.00;44563.00;44564.00;44565.00;44566.00;44562.00;44564.00];
[out ia ic]=unique(date1)
i think you are looking for ic value. see here, 44562.00 is repeated 2 times in index 1 and index 6. so ic returns 1 in index 1 and index 6. 44564.00 also repeated 2 times. check the ic value.
you can check more here
Learning
on 19 Mar 2022
You can swap the input arguments to ismember. check the value in Locb
date1=[44562 44563 44564 44563 44562 44565 44566 44567 44568 44569 44570 44571]';
date2=[44562 44563 44564 44563 44562];
[Lia,Locb]=ismember(date1,date2)
Categories
Find more on Logical 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!