find intersect of a column against another column and extracting rows
조회 수: 8 (최근 30일)
이전 댓글 표시
I'm trying to compare two tables A & B and find intersections of the time within one column of A against the time column of B and then extract that row associated with that time in that column of A & B into another array/matrix in sequential order.
CUM='/Users/jl/Desktop/Test/rev2.xlsx';
xlsread(CUM);
date1=ans(:,1);
date2=ans(:,3);
CU1=ans(:,2);
CU2=ans(:,4);
A=[date1,CU1];
B=[date2,CU2];
C=intersect(A,B);
This is what I have and the function intersect (A,B) is only extracting rows that are the same in both columns. Is there another function that can do what I'm trying to do?
댓글 수: 2
Image Analyst
2017년 6월 25일
You forgot to attach rev2.xlsx. And I'm not sure what you want. intersect() gives you the indexes where the number is common to both A and B. Then you can extract those rows from one or both matrices. Isn't that what you want?
채택된 답변
Image Analyst
2017년 6월 25일
This will do that:
D=[1,1;1,2;2,3;3,4;5,3]
F=[1,2;1,3;3,3;4,5]
commonNumbersInCol1 = intersect(D(:, 1), F(:, 1))
rowsInD = ismember(D(:, 1), commonNumbersInCol1)
rowsInF = ismember(F(:, 1), commonNumbersInCol1)
G = [D(rowsInD, :), F(rowsInF, 2)]
but what if there are not the same number of rows in both F and D? What if 1, and 3 occurred 3 times in D (like now), but 100 times in a different F? What then?
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!