comparing two tables or datasets

606 Ansichten (letzte 30 Tage)
gg am 4 Feb. 2014
Kommentiert: Peter Perkins am 14 Dez. 2017
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)

Antworten (5)

alexandre iolov
alexandre iolov am 23 Jan. 2015
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.

Azzi Abdelmalek
Azzi Abdelmalek am 4 Feb. 2014
Bearbeitet: Azzi Abdelmalek am 4 Feb. 2014
You can access your data then compare them
  2 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 4 Feb. 2014
If you have numeric data, you can use

Melden Sie sich an, um zu kommentieren.

Sean de Wolski
Sean de Wolski am 4 Feb. 2014
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,...
Height = flipud(Height);
T2 = table(Age,Height,Weight,BloodPressure,...
props = T2.Properties.VariableNames;
for ii = numel(props):-1:1
eqvars(ii) = isequal(T1.(props{ii}),T2.(props{ii}));
Then look at
Which will be true everywhere the variables are equal.
  4 Kommentare
Sean de Wolski
Sean de Wolski am 5 Feb. 2014
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)}
Which seems reasonable enough if two tables have all of the same variables..

Melden Sie sich an, um zu kommentieren.

Markus Leuthold
Markus Leuthold am 22 Mai 2015
if isequal(s1,s2)
disp('table1 and table2 are equal')
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
instead of
  4 Kommentare
Peter Perkins
Peter Perkins am 14 Dez. 2017
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.

Melden Sie sich an, um zu kommentieren.

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

Community Treasure Hunt

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

Start Hunting!

Translated by