setdiff
设置两个阵列的差异
Syntax
描述
例子
两个向量的差
定义两个具有共同值的向量。
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
差异Two Tables
定义两个共有行的表。
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 thesetOrder
argument 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的向量的差异
定义两个载体NaN
。
a = [5 nan nan];b = [5 nan];
Find the set difference ofA
和B
。
c = setDiff(a,b)
C =1×2NaN NaN
setdiff
零食NaN
值是不同的。
Cell Array of Character Vectors with Trailing White Space
创建一个字符向量的单元格数组,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
将角色向量的细胞阵列中的尾随空间视为不同的字符。
字符矢量的char和细胞阵列的差异
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'
Preserve Legacy Behavior of setdiff
Use the'遗产'
标志以保留行为setdiff
from R2012b and prior releases in your code.
找到差异A
和B
with 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
找到差异A
和B
,并保留遗产行为。
[C2,ia2] = setdiff(A,B,'遗产')
C2 =1×31 3 5
Ia2 =1×37 1 5
Input Arguments
a,b
—Input arrays
arrays
输入数组。如果指定'rows'
option, thenA
和B
必须具有相同数量的列。
A
和B
must 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.
还有其他要求A
和B
基于数据类型:
如果
A
和B
are both ordinal categorical arrays, they must have the same sets of categories, including their order. If neitherA
也不B
是有序的,它们不需要具有相同的类别集,并且使用类别名称进行比较。在这种情况下,类别C
consist of the categories ofA
其次是类别B
不在A
。The categories are in the same order as inA
和B
,类别顺序用于排序C
。如果
A
和B
are 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.如果
A
和B
are datetime arrays, they must be consistent with each other in whether they specify a time zone.
A
和B
也可以是具有以下类方法的对象:
种类
(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,A
和B
可以是图形对象的手柄数组。
setOrder
—订单标志
“排序”
(默认)|'stable'
订单标志,指定为“排序”
或者'stable'
, indicates the order of the values (or rows) inC
。
旗帜 | 描述 |
---|---|
“排序” |
中的值(或行) Example c = setDiff([4 1 3 2 5],[2 1],,“排序”)
C = 3 4 5 |
'stable' |
中的值(或行) Example c = setDiff([4 1 3 2 5],[2 1],,'stable')
C = 4 3 5 |
数据类型:char
|细绳
输出参数
C
- 差异A
和B
向量|矩阵|表|时间表
差异A
和B
, returned as a vector, matrix, table, or timetable. If the inputsA
和B
是表或时间表,然后是变量的顺序C
与变量的顺序相同A
。
以下描述了C
当输入是向量或矩阵时,当'遗产'
标志未指定:
如果the
'rows'
标志未指定,并且A
is a row vector, thenC
is a row vector.如果the
'rows'
标志未指定,并且A
不是行矢量,那么C
是列矢量。如果the
'rows'
指定标志,然后C
是包含行的矩阵A
不在B
。如果all the values (or rows) of
A
也在B
, thenC
is an empty matrix.
班级C
与班级相同A
, unless:
A
is a character array andB
的特征向量,是一个单元阵列caseC
是字符矢量的单元格数组。A
is a character vector, cell array of character vectors, or string, andB
是一个分类数组,在这种情况下C
是一个分类阵列。A
是字符矢量或单个字符向量的单元格数组,B
是DateTime数组,在这种情况下C
是DateTime数组。A
是字符向量的字符向量或单元格数组,B
是字符串数组,在这种情况下C
是字符串数组。
ia
- 索引A
列向量
索引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 use
setdiff(A(:,
, 在哪里var
),b(:,,,var
))var
是一个正整数,正整数的向量,一个可变名称,可变名称的单元格数组或逻辑向量。或者,您可以使用vartype
创建一个选择指定类型的变量的下标。
扩展功能
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
C/C ++代码生成
使用MATLAB®CODER™生成C和C ++代码。
用法注释和限制:
Code generation does not support cell arrays for the first or second arguments.
当您不指定
'rows'
选项:Inputs
A
和B
必须是向量。如果指定'遗产'
option, then inputsA
和B
必须是行向量。可变大小行向量的第一维必须具有固定的长度1.变量大小列向量的第二维必须具有固定的长度1。
Do not use
[]
至represent the empty set. Use a 1-by-0 or 0-by-1 input, for example,零(1,0)
,代表空集。如果指定
'遗产'
选项,然后空输出为行向量,1 by-0。他们从来没有0 x-0。
When you specify both the
'遗产'
和'rows'
选项,输出ia
是列矢量。如果ia
is empty, then it is 0-by-1. It is never 0-by-0, even if the outputC
is 0-by-0.当。。。的时候
setOrder
不是'stable'
或指定'遗产'
选项,必须已经按顺序排序输入。第一个输出,C
, is sorted in ascending order.Complex inputs must be
单身的
或者双倍的
。当一个输入是复杂的,另一个输入是真实的,请执行以下操作之一:
Set
setOrder
至'stable'
。Sort the real input in complex ascending order (by absolute value). Suppose the real input is
x
。Use种类(complex(x))
或者sortrows(复杂(x))
。
SeeCode Generation for Complex Data with Zero-Valued Imaginary Parts(MATLAB编码器)。
基于线程的环境
Run code in the background using MATLAB®背景池
或使用并行计算工具箱加速代码™螺纹池
。
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment。
GPU数组
使用并行计算工具箱™在图形处理单元(GPU)上运行加速代码。
用法注释和限制:
The
'遗产'
flag is not supported.不支持64位整数。万博1manbetx
有关更多信息,请参阅Run MATLAB Functions on a GPU(并行计算工具箱)。
Version History
matlab命令
You clicked a link that corresponds to this MATLAB command:
通过在MATLAB命令窗口中输入该命令。Web浏览器不支持MATLAB命令。万博1manbetx
您还可以从以下列表中选择一个网站:
如何获得最佳网站性能
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- AméricaLatina(Español)
- Canada(英语)
- United States(英语)