MATLAB Answers

Setdiff with two datasets, looking at only certain columns

20 views (last 30 days)
I have two datasets, they have the same column headings and different number of rows. I want to create a new dataset that pulls out what they have in common (intersect) and also what is unique in one versus the other (setdiff).
I would use what I have written above, except that I want to focus in on two columns and pull out the entire row of data.
*Dataset 1:*
Name Position Letter
1 587 A
1 848 A
5 398 B
*Dataset 2:*
Name Position Letter
1 587 B
1 848 C
2 3188 B
8 3282 G
I want to generate a new dataset that looks only at the first and second column to generate new datasets.
For example I want to generate the following if I am looking for "unique" rows present in dataset 1, but not dataset 2:
Dataset 3:
Name Position Letter
5 398 B
Whereas I would generate the following if I am looking for common rows present in dataset 1 that are also in dataset 2:
Dataset 4
Name Position Letter
1 587 A
1 848 A
Even suggestions for commands to look at would be appreciated. Thanks in advance.

Accepted Answer

Ramnarayan Krishnamurthy
Ramnarayan Krishnamurthy on 28 Dec 2017
Edited: Ramnarayan Krishnamurthy on 28 Dec 2017
You can use the ' setdiff' and ' intersect' functions with logical indexing to get the output you desire.
As an example:
% Set up the tables
T = table([1;1;5],[587;848;398],{'A';'A';'B'},'VariableNames',{'Name','Position','Letter'})
T2 = table([1;1;2;8],[587;848;3188;3282],{'B';'C';'B';'G'},'VariableNames',{'Name','Position','Letter'})
% The second output argument is the index (Row) in Table T
% We are "looking" at the first 2 columns only
[~,index1] = setdiff(T(:,1:2),T2(:,1:2))
% Displaying only the appropriate entry
T(index1,:)
% Same syntax as setdiff
[~,index2] = intersect(T(:,1:2),T2(:,1:2))
% Displaying only the appropriate entry
T(index2,:)
HTH

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by