6

I'm currently migrating code from R2012a to R2013b.

I noticed that the unique function behavior has changed:

R2012a

>> size(unique([]))

ans =

     0     0

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. Does anybody has a rationale for this?

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
Eric Salemi
  • 119
  • 9
  • 2
    This is possibly related: [Iterating an empty matrix using a for loop](http://stackoverflow.com/questions/17950940/iterating-an-empty-matrix-using-a-for-loop) – Dennis Jaheruddin Dec 05 '13 at 11:15

2 Answers2

7

The behaviour has changed with R2013a, if you need the old behaviour use:

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.

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

Daniel
  • 36,610
  • 3
  • 36
  • 69
1

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.

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.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122