# comparing two tables or datasets

조회 수: 499 (최근 30일)
gg . 2014년 2월 4일
댓글: Peter Perkins . 2017년 12월 14일
The question relates to datasets as well as tables. It is stated in the following for datasets only. Assume you have two datasets dsA and dsB, which are of equal dimension with identical variables but are unequal (i.e.
isequal(dsA, dsB)
evaluates "false".
Is there a better way to find the actual row(s) and column(s) (or variable(s)) which are different, than the obvious and tedious one? (Obvious and tedious is to check dimensions, if equal check each variable, apply proper "eq" to each variable)

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

### 답변 (5개)

alexandre iolov 2015년 1월 23일
Look into setdiff(dsA, dsB),
'''If A and B are tables, then setdiff returns the rows from A that are not in B, with repetitions removed. The rows of table C are in sorted order.'''
however, beware that nan~=nan and similarly for entries of categorical arrays which are undefined. undefined ~= undefined
There might be other gotchas that I don't know of, please share.
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

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

Azzi Abdelmalek 2014년 2월 4일
편집: Azzi Abdelmalek 님. 2014년 2월 4일
You can access your data then compare them
dsA={1;2;3};
dsA=mat2dataset(dsA);
dsB={1;2;3};
dsB=mat2dataset(dsB)
%compare
sign(dsA.dsA1-dsB.dsB1)
##### 댓글 수: 2없음 표시없음 숨기기
gg 2014년 2월 4일
I do not think this works for variables where "-" is not defined.
Azzi Abdelmalek 2014년 2월 4일
If you have numeric data, you can use
sign(cell2mat(dsA.dsA1)-cell2mat(dsB.dsB1))

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

Sean de Wolski 2014년 2월 4일
Use a for-loop to loop over the variables and test equality:
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
% Two tables, Height is different
T1 = table(Age,Height,Weight,BloodPressure,...
'RowNames',LastName);
Height = flipud(Height);
T2 = table(Age,Height,Weight,BloodPressure,...
'RowNames',LastName);
props = T2.Properties.VariableNames;
for ii = numel(props):-1:1
eqvars(ii) = isequal(T1.(props{ii}),T2.(props{ii}));
end
Then look at
eqvars
Which will be true everywhere the variables are equal.
##### 댓글 수: 4이전 댓글 2개 표시이전 댓글 2개 숨기기
gg 2014년 2월 5일
This is (part of) what I mentioned in my question as the "obvious but tedious" solution. I was surprised that objects like table or dataset do not come with an "isequal" method able to handle this. I just wanted to make sure I haven't overlooked something.
Sean de Wolski 2014년 2월 5일
My interpretation is that you want varfun to work like cellfun in that cellfun can work on two cell arrays at once:
C1 = {1, pi, magic(3)}
C2 = {2, pi, magic(3)}
cellfun(@(x,y)x-y,C1,C2,'UniformOutput',false)
Which seems reasonable enough if two tables have all of the same variables..

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

Markus Leuthold 2015년 5월 22일
table1=table(....)
table2=table(....)
%comparison
s1=table2struct(table1)
s2=table2struct(table2)
if isequal(s1,s2)
disp('table1 and table2 are equal')
end
However, I agree there should be an overloaded function isequal in the table class. As a previous commenter already said: If you expect NaN in your table, you have to use
isequaln
isequal
##### 댓글 수: 4이전 댓글 2개 표시이전 댓글 2개 숨기기
Guillaume 2017년 12월 7일
Can you provide a recipe for that. It seems to work fine for me:
>>t1 = table([1;2;3]);
>>t2 = table([1;2;3]);
>>isequaln(t1, t2)
ans =
1
Peter Perkins 2017년 12월 14일
There are no pointers involved in my code, t1 and t2 are completely different instances. Perhaps you have missing values in your tables, in which case you'll probably want to use isequaln.

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

Dan 2017년 2월 27일
isequaln(table1 ,table2) possibly your error derives from the fact that if there are nans isequal will show false.

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

### 카테고리

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

### Community Treasure Hunt

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

Start Hunting!