find intersect of a column against another column and extracting rows

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 Comments

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?
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.

Sign in to comment.

 Accepted Answer

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?

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!