Matlab: unique function behavior

I am currently porting code from R2012a to R2013b.

I noticed that the behavior of the unique function has changed:

R2012a

 >> size(unique([])) ans = 0 0 

R2013b

 >> size(unique([])) ans = 0 1 

It seems inconsistent for me that the 0x0 matrix will become the 0x1 matrix after removing the doubles, which is essentially what the unique function does. Does anyone have a rationale for this?

+6
source share
2 answers

The behavior has changed since R2013a if you need to use the old behavior:

size(unique([],'legacy'))

If you need code for both versions, I would recommend writing some function that calls unique(x,'legacy') for new versions and unique(x) for old versions.

btw: same issue with union , intersect , setdiff , setxor and ismember

+7
source

I do not know if this is the reason, but it has an advantage.

Now you will see that unique(M) gives the same result as unique(M(:)) , even if M empty.

Example:

 M = magic(5); isequal(size(unique(M)), size(unique(M(:)))); M = []; isequal(size(unique(M)), size(unique(M(:)))); 

The latter returns false in older versions of matlab, this can be confusing.

+1
source

Source: https://habr.com/ru/post/959374/


All Articles