find intersect of a column against another column and extracting rows

6 次查看(过去 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 中查找有关 Adding custom doc 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by