主要内容

setdiff

设置两个阵列的差异

描述

example

C= setdiff(a,b)返回数据A那不在B,没有重复。C按顺序排序。

  • 如果ABare tables or timetables, thensetdiffreturns the rows fromA不在B。对于时间表,setdiff考虑排时以确定平等,并分类输出时间表Cby row times.

example

C= setdiff(a,b,setOrder)returnsC按照特定顺序。setOrdercan be“排序”或者'stable'

C= setdiff(a,b,___,“行”)C= setdiff(a,b,“行”,___)对待每一行A和each row ofB作为单个实体并从A不在B,没有重复。You must specifyAB并且可以选择指定setOrder

The'rows'option does not support cell arrays, unless one of the inputs is either a categorical array or a datetime array.

example

[C,ia] = setDiff(___)还返回索引向量ia使用任何先前的语法。

  • 一般来说,c = a(ia)

  • 如果the'rows'指定选项,然后c = a(ia,:)

  • 如果ABare tables or timetables, thenc = a(ia,:)

example

[C,ia] = setDiff(a,b,'遗产')[C,ia] = setDiff(a,b,“行”,“遗产”)preserve the behavior of thesetdifffunction from R2012b and prior releases.

The'遗产'选项不支持分类数组,日期时间数组万博1manbetx,持续时间数组,表格或时间表。

例子

collapse all

定义两个具有共同值的向量。

a = [3 6 2 1 5 1 1];b = [2 4 6];

Find the values inA不在B

c = setDiff(a,b)
C =1×31 3 5

定义两个共有行的表。

a =表([1:5]',['一个';'b';'C';'D';'e'],逻辑([0; 1; 0; 1; 1; 0]))))
A=5×3桌var1 var2 var3 ____ ____ _____ 1 a false 2 b true 3 c false 4 d true 5 e false
b =表([1:2:10]',['一个';'C';'e';'G';'我'],逻辑(零(5,1)))))
B=5×3桌var1 var2 var3 ____ ____ _____ 1 a false 3 c false 5 e false 7 g false 9 i false

找到行A不在B

c = setDiff(a,b)
C=2×3桌var1 var2 var3 ____ ____ _____ 2 b true 4 d true

定义两个具有共同值的向量。

a = [3 6 2 1 5 1 1];b = [2 4 6];

Find the values inA不在B以及索引向量ia,这样c = a(ia)

[C,ia] = setdiff(A,B)
C =1×31 3 5
ia =3×14 1 5

Define a table,A,五个人的性别,年龄和身高。

A = table(['M';'M';'F';'M';'F'],[27; 52; 31; 46; 35],[74; 68; 64; 64; 61; 64] ,,...'variablenames',{'性别''年龄''高度'},,...'Rownames',{'ted'“弗雷德”“贝蒂”'鲍勃''朱迪'})
A=5×3桌Gender Age Height ______ ___ ______ Ted M 27 74 Fred M 52 68 Betty F 31 64 Bob M 46 61 Judy F 35 64

Define a table,B, with the same variables asA

b =表(['F';'M';'F';'F'],[64; 68; 62; 58],[31; 47; 35; 23],,...'variablenames',{'性别''高度''年龄'},,...'Rownames',{'Meg''乔''贝丝''艾米'})
B=4×3桌Gender Height Age ______ ______ ___ Meg F 64 31 Joe M 68 47 Beth F 62 35 Amy F 58 23

找到行A不在B, as well as the index vectoria,这样c = a(ia,:)

[C,ia] = setdiff(A,B)
C=4×3桌性别年龄高度______ _______________________________________________________________________________________________________________________
ia =4×15 1 4 2

C首先按顺序排序Gender和NExt byAge

定义两个共有行的矩阵。

a = [7 9 7;0 0 0;7 9 7;5 5 5;1 4 5];b = [0 0 0;5 5 5];

A不在B以及索引向量ia,这样c = a(ia,:)

[c,ia] = setDiff(a,b,'rows')
C =2×31 4 5 7 9 7
ia =2×15 1

Use thesetOrderargument to specify the ordering of the values inC

指定'stable'或者“排序”当值的顺序C是重要的。

a = [3 6 2 1 5 1 1];b = [2 4 6];[c,ia] = setDiff(a,b,'stable')
C =1×33 1 5
ia =3×11 4 5

Alternatively, you can specify“排序”命令。

[c,ia] = setDiff(a,b,“排序”)
C =1×31 3 5
ia =3×14 1 5

定义两个载体NaN

a = [5 nan nan];b = [5 nan];

Find the set difference ofAB

c = setDiff(a,b)
C =1×2NaN NaN

setdiff零食NaN值是不同的。

创建一个字符向量的单元格数组,A

a = {'狗','猫','鱼','horse'};

创建一个字符向量的单元格数组,B, 在哪里some of the vectors have trailing white space.

b = {'狗 ','猫','fish ','horse'};

Find the character vectors inA不在B

[C,ia] = setdiff(A,B)
C =1x2 cell{'dog'} {'fish'}
ia =2×11 3

setdiff将角色向量的细胞阵列中的尾随空间视为不同的字符。

Create a character vector,A

a = ['猫';'狗';'狐狸';'猪'];班级(A)
ans ='char'

创建一个字符向量的单元格数组,B

b = {'狗','猫','鱼','horse'};(b)类
ans ='cell'

Find the character vectors inA不在B

c = setDiff(a,b)
C =2x1单元{'fox'} {'猪'}

结果,C,是字符矢量的单元格数组。

class(C)
ans ='cell'

Use the'遗产'标志以保留行为setdifffrom R2012b and prior releases in your code.

找到差异ABwith the current behavior.

a = [3 6 2 1 5 1 1];b = [2 4 6];[C1,IA1] = setDiff(a,b)
C1 =1×31 3 5
Ia1 =3×14 1 5

找到差异AB,并保留遗产行为。

[C2,ia2] = setdiff(A,B,'遗产')
C2 =1×31 3 5
Ia2 =1×37 1 5

Input Arguments

collapse all

输入数组。如果指定'rows'option, thenAB必须具有相同数量的列。

ABmust belong to the same class with the following exceptions:

  • logical,char, and all numeric classes can combine with双倍的数组。

  • 字符矢量的单元格数可以与字符数组或字符串数​​组结合使用。

  • Categorical arrays can combine with character arrays, cell arrays of character vectors, or string arrays.

  • Datetime arrays can combine with cell arrays of date character vectors or single date character vectors.

还有其他要求AB基于数据类型:

  • 如果ABare both ordinal categorical arrays, they must have the same sets of categories, including their order. If neitherA也不B是有序的,它们不需要具有相同的类别集,并且使用类别名称进行比较。在这种情况下,类别Cconsist of the categories ofA其次是类别B不在A。The categories are in the same order as inAB,类别顺序用于排序C

  • 如果ABare tables or timetables, they must have the same variable names (except for order). For tables, row names are ignored, so that two rows that have the same values, but different names, are considered equal. For timetables, row times are taken into account, so that two rows that have the same values, but different times, are not considered equal.

  • 如果ABare datetime arrays, they must be consistent with each other in whether they specify a time zone.

AB也可以是具有以下类方法的对象:

  • 种类(orsor为了'rows'选项)

  • eq

  • NE

The object class methods must be consistent with each other. These objects include heterogeneous arrays derived from the same root class. For example,AB可以是图形对象的手柄数组。

订单标志,指定为“排序”或者'stable', indicates the order of the values (or rows) inC

旗帜 描述
“排序”

中的值(或行)C按返回的分类顺序返回种类

Example

c = setDiff([4 1 3 2 5],[2 1],,“排序”)
C = 3 4 5

'stable'

中的值(或行)C以与中的顺序相同的顺序返回A

Example

c = setDiff([4 1 3 2 5],[2 1],,'stable')
C = 4 3 5

数据类型:char|细绳

输出参数

collapse all

差异AB, returned as a vector, matrix, table, or timetable. If the inputsAB是表或时间表,然后是变量的顺序C与变量的顺序相同A

以下描述了C当输入是向量或矩阵时,当'遗产'标志未指定:

  • 如果the'rows'标志未指定,并且Ais a row vector, thenCis a row vector.

  • 如果the'rows'标志未指定,并且A不是行矢量,那么C是列矢量。

  • 如果the'rows'指定标志,然后C是包含行的矩阵A不在B

  • 如果all the values (or rows) ofA也在B, thenCis an empty matrix.

班级C与班级相同A, unless:

  • Ais a character array andB的特征向量,是一个单元阵列caseC是字符矢量的单元格数组。

  • Ais a character vector, cell array of character vectors, or string, andB是一个分类数组,在这种情况下C是一个分类阵列。

  • A是字符矢量或单个字符向量的单元格数组,B是DateTime数组,在这种情况下C是DateTime数组。

  • A是字符向量的字符向量或单元格数组,B是字符串数组,在这种情况下C是字符串数组。

索引A,当'遗产'标志未指定。ia确定在A不在B。如果there is a repeated value (or row) appearing exclusively inA, thenia包含第一次出现值(或行)的索引。

提示

  • To find the set difference with respect to a subset of variables from a table or timetable, you can use column subscripting. For example, you can usesetdiff(A(:,var),b(:,,,var)), 在哪里var是一个正整数,正整数的向量,一个可变名称,可变名称的单元格数组或逻辑向量。或者,您可以使用vartype创建一个选择指定类型的变量的下标。

扩展功能

Version History

在R2006a之前引入