Main Content

setdiff.

组difference of two arrays

Description

C= setdiff(一种那B.回报the data in一种that is not inB.,没有重复。C我s in sorted order.

  • 如果一种andB.是表或时间表,然后setdiff.返回行的行一种that are not inB.。For timetables,setdiff.takes row times into account to determine equality, and sorts the output timetableC按行时间。

C= setdiff(一种那B.setOrder回报C我n a specific order.setOrder'sorted'or'稳定'

C= setdiff(一种那B.___那'行')andC= setdiff(一种那B.那'行'那___treat each row of一种和每一行B.as single entities and return the rows from一种that are not inB.,没有重复。您必须指定一种andB.and optionally can specifysetOrder

'行'选项不支持单元格阵列,除非其中一万博1manbetx个输入是分类数组或DATETIME数组。

[CIA.] = setdiff(___还返回索引矢量IA.using any of the previous syntaxes.

  • Generally,C= A(ia)

  • 如果是'行'option is specified, thenC= A(ia,:)

  • 如果一种andB.是表或时间表,然后C= A(ia,:)

[CIA.] = setdiff(一种那B.那'legacy')and[CIA.] = setdiff(一种那B.那'行'那'legacy')保留行为setdiff.来自R2012B和先前版本的功能。

'legacy'option does not support categorical arrays, datetime arrays, duration arrays, tables, or timetables.

例s

全部收缩

Define two vectors with values in common.

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

找到值一种that are not inB.

C= setdiff(一种那B.)
C=1×31 3.5

Define two tables with rows in common.

a =表([1:5]',['A';'B';'C';'D';'E'],logical([0;1;0;1;0]))
A =5×3 tableVar1 Var2 Var3 ____ ____ _____ 1 A false 2 B true 3 C false 4 D true 5 E false
B.= table([1:2:10]',['A';'C';'E';'G';'I'],logical(zeros(5,1)))
B =5×3 tableVar1 Var2 Var3 ____ ____ _____ 1 A false 3 C false 5 E false 7 G false 9 I false

Find the rows in一种that are not inB.

C= setdiff(一种那B.)
C =2×3 tableVar1 Var2 Var3 ____ ____ _____ 2 B true 4 D true

Define two vectors with values in common.

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

找到值一种that are not inB.as well as the index vectorIA.那such thatC= A(ia)

[c,ia] = setdiff(a,b)
C=1×31 3.5
IA.=3×14 1 5.

定义表,一种那of gender, age, and height for five people.

a =表(['M';'M';'F';'M';'F'],[27;52;31;46;35],[74;68;64;61;64],。。。'VariableNames'那{'Gender''Age''Height'},。。。'RowNames'那{'Ted''Fred''Betty''Bob''Judy'})
A =5×3 table性别年龄Height ______ ___ ______ Ted M 27 74 Fred M 52 68 Betty F 31 64 Bob M 46 61 Judy F 35 64

定义表,B.,具有相同的变量一种

B.= table(['F';'M';'F';'F'],[64;68;62;58],[31;47;35;23],。。。'VariableNames'那{'Gender''Height''Age'},。。。'RowNames'那{'Meg''Joe''Beth''Amy'})
B =4×3 table性别高度______ ______ ___ ___ meg f 64 31 joe m 68 47 beth f 62 35 amy f 58 23

Find the rows in一种that are not inB.以及索引矢量IA.那such thatC= A(ia,:)

[c,ia] = setdiff(a,b)
C =4×3 table性别年龄Height ______ ___ ______ Judy F 35 64 Ted M 27 74 Bob M 46 61 Fred M 52 68
IA.=4×15 1 4 2

该rows ofCare in sorted order first by性别下一个年龄

Define two matrices with rows in common.

一种= [7 9 7; 0 0 0; 7 9 7; 5 5 5; 1 4 5]; B = [0 0 0; 5 5 5];

Find the rows from一种that are not inB.as well as the index vectorIA.那such thatC= A(ia,:)

[c,ia] = setdiff(a,b,'行'
C=2×31 4 5.7 9 7
IA.=2×15 1

使用setOrder参数指定值的排序C

Specify'稳定'or'sorted'when the order of the values inCare important.

a = [3 6 2 1 5 1];B = [2 4 6];[c,ia] = setdiff(a,b,'稳定'
C=1×33 1 5.
IA.=3×11 4 5.

或者,您可以指定'sorted'order.

[c,ia] = setdiff(a,b,'sorted'
C=1×31 3.5
IA.=3×14 1 5.

Define two vectors containing

一种= [5 NaN NaN]; B = [5 NaN];

找到设定的差异一种andB.

C= setdiff(一种那B.)
C=1×2南纳

setdiff.treatsvalues as distinct.

Create a cell array of character vectors,一种

一种= {'狗''cat''fish''马'};

Create a cell array of character vectors,B.,其中一些矢量有尾随空格。

B.= {'dog ''cat'“鱼”'马'};

找到字符向量一种that are not inB.

[c,ia] = setdiff(a,b)
C=1x2细胞{'狗'} {'鱼'}
IA.=2×11 3.

setdiff.treats trailing white space in cell arrays of character vectors as distinct characters.

创建一个字符向量,一种

一种= ['cat';'狗';'fox';'pig']; class(A)
ans = 'char'

Create a cell array of character vectors,B.

B ={'狗''cat''fish''马'}; class(B)
ans = 'cell'

找到字符向量一种that are not inB.

C= setdiff(一种那B.)
C=2x1 cell{'fox'} {'pig'}

该result,C那我s a cell array of character vectors.

班级(c)
ans = 'cell'

使用'legacy'flag to preserve the behavior ofsetdiff.来自R2012B和代码中的先前版本。

Find the difference of一种andB.随着当前的行为。

a = [3 6 2 1 5 1];B = [2 4 6];[C1,ia1] = setdiff(A,B)
C1 =1×31 3.5
IA.1 =3×14 1 5.

Find the difference of一种andB.那and preserve the legacy behavior.

[C2,IA2] = SetDiff(A,B,'legacy'
C2 =1×31 3.5
IA.2 =1×37 1 5

输入参数

全部收缩

输入阵列那specified as numeric arrays, logical arrays, character arrays, string arrays, categorical arrays, datetime arrays, duration arrays, cell arrays of character vectors, tables, or timetables. If you specify the'行'option,一种andB.must have the same number of columns.

一种andB.必须属于具有以下例外的同一类:

  • 逻辑char,所有数字类都可以与之结合doublearrays.

  • Cell arrays of character vectors can combine with character arrays or string arrays.

  • 分类阵列可以与字符数组,字符向量的单元格阵列或字符串数​​组组合。

  • DateTime阵列可以与日期字符向量或单日日期矢量的单元格阵列组合。

该re are additional requirements for一种andB.based on data type:

  • 如果一种andB.是序列分类阵列,它们必须具有相同的类别,包括其订单。如果既不是一种norB.are ordinal, they need not have the same sets of categories, and the comparison is performed using the category names. In this case, the categories ofCare the sorted union of the categories from一种andB.

  • 如果一种andB.是表或时间表,它们必须具有相同的变量名称(订单除外)。对于表,忽略行名称,因此具有相同的两行具有相同值但不同的名称。对于时间表,考虑行时间,因此两行具有相同值但不同的时间,不被视为相等。

  • 如果一种andB.是DateTime阵列,它们必须在彼此中一致,无论是指定时区。

一种andB.also can be objects with the following class methods:

  • sort(要么sortrowsfor the'行'option)

  • eq.

  • ne

对象类方法必须彼此一致。这些对象包括来自相同根类的异构阵列。例如,一种andB.可arrays of handles to graphics objects.

Order flag, specified as'sorted'or'稳定',表示值(或行)的顺序C

Flag Description
'sorted'

该values (or rows) inCreturn in sorted order as returned bysort

C= setdiff([4 1 3 2 5],[2 1],'sorted'
C= 3 4 5

'稳定'

该values (or rows) inCreturn in the same order as in一种

C= setdiff([4 1 3 2 5],[2 1],'稳定'
C= 4 3 5

Data Types:char|string

Output Arguments

全部收缩

的差异一种andB.,作为向量,矩阵,表格或时间表返回。如果输入一种andB.是表或时间表,然后the order of the variables inC我s the same as the order of the variables in一种

该following describes the shape ofCwhen the inputs are vectors or matrices and when the'legacy'flag is not specified:

  • 如果是'行'flag is not specified and一种是一排矢量,然后C是一排矢量。

  • 如果是'行'flag is not specified and一种我s not a row vector, thenC我s a column vector.

  • 如果是'行'flag is specified, thenC我s a matrix containing the rows of一种that are not inB.

  • 如果所有的值(或行)一种are also inB., 然后C是一个空矩阵。

该class ofC我s the same as the class of一种,除非:

  • 一种是一个字符阵列和B.是一个字符向量的单元格阵列,在这种情况下C我s a cell array of character vectors.

  • 一种是一个字符矢量,字符矢量或字符串的特征阵列,以及B.我s a categorical array, in which caseC我s a categorical array.

  • 一种我s a cell array of character vectors or single character vector andB.我s a datetime array, in which caseC我s a datetime array.

  • 一种我s a character vector or cell array of character vectors andB.我s a string array, in which caseC我s a string array.

Index to一种那returned as a column vector when the'legacy'flag is not specified.IA.我dentifies the values (or rows) in一种that are not inB.。如果有一个专门出现的重复值(或行)一种, 然后IA.contains the index to the first occurrence of the value (or row).

Tips

  • 要找到与表或时间表的变量子集的集合差异,可以使用列订户。例如,您可以使用setdiff(a(:,vars)那B.(:,vars)))那wherevars我s a positive integer, a vector of positive integers, a variable name, a cell array of variable names, or a logical vector. Alternatively, you can usevartype.to create a subscript that selects variables of a specified type.

Extended Capabilities

Introduced before R2006a