[英]Matlab: Behavior of the unique function
I'm currently migrating code from R2012a to R2013b. 我目前正在将代码从R2012a迁移到R2013b。
I noticed that the unique
function behavior has changed: 我注意到unique
功能行为已经改变:
R2012a R2012a
>> size(unique([]))
ans =
0 0
R2013b R2013b
>> size(unique([]))
ans =
0 1
It seems counter-intuitive to me that a 0x0 matrix would become a 0x1 matrix after removing doublons, which is essentially what the unique function does. 对我来说,在删除双重后,0x0矩阵将成为0x1矩阵似乎是违反直觉的,这实际上是唯一函数的作用。 Does anybody has a rationale for this? 有没有人有理由这样做?
The behaviour has changed with R2013a, if you need the old behaviour use: 如果您需要使用旧行为,则R2013a的行为已更改:
size(unique([],'legacy'))
If you need code for both versions, I would recommend to write some function which calls unique(x,'legacy')
for new versions and unique(x)
for old versions. 如果您需要两个版本的代码,我建议编写一些函数,为新版本调用unique(x,'legacy')
,为旧版本调用unique(x)
。
btw: same issue with union
, intersect
, setdiff
, setxor
and ismember
顺便说一句:与union
, intersect
, setdiff
, setxor
和ismember
相同的问题
I don't know whether this is the reason, but it does come with an advantage. 我不知道这是否是原因,但它确实带来了优势。
Now you will see that unique(M)
gives the same output as unique(M(:))
, even if M
is empty. 现在您将看到unique(M)
提供与unique(M(:))
相同的输出,即使M
为空。
Example: 例:
M = magic(5);
isequal(size(unique(M)), size(unique(M(:))));
M = [];
isequal(size(unique(M)), size(unique(M(:))));
The latter returns false on old versions of matlab, this may be confusing. 后者在旧版本的matlab上返回false,这可能会令人困惑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.