0

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.

4

2 回答 2

1

没有什么。如果输入AB是具有相同变量的表,我们有:

具有相同值但名称不同的两行被视为相等。

因此,'rows'默认情况下包含该选项。实际上,intersectintersect方法(@table\intersect.m)中的底层调用是:

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

只需要使用它来区分数组元素或数组行的相交。例如

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

对比

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

C =

     1     2     3
     2     2     2
于 2016-07-01T02:33:58.630 回答
0

如果您阅读文档,对于intersect(a,b)它说的表格:

如果 A 和 B 是表,则 intersect 返回两个表共有的行集。

对于intersect(a,b,'rows')它说的表格:

将 A 的每一行和 B 的每一行视为单个实体,并返回 A 和 B 共有的行。

因此,可以得出结论,对于,操作是相同的,正如您所演示的那样。但是,如果使用普通的旧数组,则行为会有所不同:

a = randi(10, 10)
intersect(a, a)
ans = 
    1
    2
  ...
   10

在这里,我们将数组展平并寻找相同的单元格。

intersect(a, a, 'rows')
ans =
    9  3 ... 7
    ...

在这里,我们按行进行比较,并将结果作为完整匹配的行返回,就好像数组是您在问题中显示的表格一样。

你也可以试试:

a = randi(10, 10)
b = randi(10, 10)
intersect(a, b)
intersect(a, b, 'rows')

并且看到第一种形式几乎总是得到 1 到 10,而第二种形式几乎从来没有得到任何匹配。

于 2016-07-01T02:34:17.507 回答