Search code examples
matlab-table

What does the 'rows' specification do in intersect(tableA,tableB,'rows')


I read the documentation upside-down and sideways. For the life of me, I can't discern what the effect of 'rows' is. Here is my test code, which also doesn't seem to be revealing.

>> a=table((1:5)',{'a';'bc';'def';'gh';'i'})
   a = Var1    Var2
       ____    _____
       1       'a'
       2       'bc'
       3       'def'
       4       'gh'
       5       'i'

>> b=a([1 2 3],:)
   b = Var1    Var2
       ____    _____
       1       'a'
       2       'bc'
       3       'def'

>> c=a([2 3 4],:)
   c = Var1    Var2
       ____    _____
       2       'bc'
       3       'def'
       4       'gh'

>> intersect(b,c)
   ans = Var1    Var2
         ____    _____
         2       'bc'
         3       'def'

>> intersect(b,c,'rows')
   ans = Var1    Var2
         ____    _____
         2       'bc'
         3       'def'

Thanks to anyone who can provide clarity on this.


Solution

  • Nothing. If the inputs A and B are tables with the same variables, we have:

    Two rows that have the same values, but different names, are considered equal.

    Thus, the 'rows' option is included by default. In fact, the underlying call to intersect in the table intersect method (@table\intersect.m) is:

    [~,ia,ib] = intersect(ainds,binds,flag,'rows');
    

    It is only necessary to use it to distinguish the intersect of elements of an array or rows of array. E.g.

    A = [2 2 2; 0 0 1; 1 2 3; 1 1 1];
    B = [1 2 3; 2 2 2; 2 2 0];
    
    >> [C, ia, ib] = intersect(A,B)
    
    C =
    
         0
         1
         2
         3
    

    vs

    >> [C, ia, ib] = intersect(A,B, 'rows')
    
    C =
    
         1     2     3
         2     2     2