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
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?
liu James
liu James 2017년 6월 25일
I've attached the file. For instance assume
D=[1,1;1,2;2,3;3,4;5,3]
this gives, and
D =
1 1
1 2
2 3
3 4
5 3
F=[1,2;1,3;3,3;4,5]
F =
1 2
1 3
3 3
4 5
I want to extract and make this.
G=[1,1,2;1,2,3;3,4,3]
G =
1 1 2
1 2 3
3 4 3
however, the intersect function intersect(D,F) would give me the list of numbers that are common in both arrays.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
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개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by