필터 지우기
필터 지우기

Intersect - extraction of row data based on matched results

조회 수: 1 (최근 30일)
Learning
Learning 2022년 3월 18일
댓글: Arif Hoq 2022년 3월 20일
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에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by