Intersect - extraction of row data based on matched results

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

回答(1 个)

Arif Hoq
Arif Hoq 2022-3-18
编辑:Arif Hoq 2022-3-18
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,:)
result = 2×3 table
a b c ______ _ _ {'t1'} 1 2 {'t3'} 1 4
  6 个评论
Learning
Learning 2022-3-19
Hi Arif, I think I'm very close. please see image below, As you can see, both 44562 and 44563 appears twice. Looking at the result table, I want the idices/row numbers to be unique...that is the rows for both 44562 appears are displayed under index as 1 and 5. The previous code would have listed the rows or indices for both 44562 as 1 and 1. Is there a way of making sure that once a particular value is selected, it's index cannot be selected again? that is once the first 44562 which is in row 1 is selected, row 1 cannot be selected again The next time 44562 is encounted, it would be row 5 and hence 5 will be the result for the other 44562. Thanks for not making things clear and I appreciate your help!
Arif Hoq
Arif Hoq 2022-3-20
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)
Lia = 12×1 logical array
1 1 1 1 1 0 0 0 0 0 0 0
Locb = 12×1
1 2 3 2 1 0 0 0 0 0

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by