File Exchange

image thumbnail

UNIQUEWITHEQUALNANS

version 1.1.0.0 (2.05 KB) by Jos (10584)
Set unique, treating NaNs as equal (v2.0, mar 2009)

1 Download

Updated 06 Apr 2009

View Version History

View License

UNIQUEWITHEQUALNANS - Set unique, treating NaNs as equal
B = UNIQUEWITHEQUALNANS(A) for the array A returns the same values as in A
but with no repetitions. B will also be sorted (NaNs as last).

UNIQUEWITHEQUALNANS(A,'rows') for the matrix A returns the unique rows
of A, treating NaNs as equal.

[B,I,J] = UNIQUEWITHEQUALNANS(...) also returns index vectors I and J such
that B = A(I) and A = B(J) (or B = A(I,:) and A = B(J,:)).

[B,I,J] = UNIQUEWITHEQUALNANS(...,'first') returns the vector I to
index the first occurrence of each unique value in A.
UNIQUEWITHEQUALNANS(...,'last'), the default, returns the vector I to
index the last occurrence.

Example:
A = [1 2 NaN ; 1 2 NaN ; 1 2 NaN] ;
unique(A,'rows')
% -> [1 2 NaN ; 1 2 NaN ; 1 2 NaN] ;
uniquewithequalnans(A,'rows')
%-> [1 2 NaN]

For a cell array of strings, this function behaves exactly like UNIQUE.

See also unique, ISEQUALWITHNANS.

v2.0, mar 2009 - update to deal with recent versions of UNIQUE

Cite As

Jos (10584) (2021). UNIQUEWITHEQUALNANS (https://www.mathworks.com/matlabcentral/fileexchange/12292-uniquewithequalnans), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

Hoi Wong

Since this code is a wrapper around the builtin/unique(), the default behavior for 'first', 'last' depends on your version of MATLAB (before R2013a or R2013a and after).

You might want to update your comments so it applies to more versions. Right now the description says 'last' is the default but the code actually behaves as 'first' when I run it in R2013a or after.

Thanks.

Jos (10584)

@Hoi Wong: apparently TMW changed the internals of UNIQUE, so it is no longer backward compatible. I will update this function shortly.

Hoi Wong

Great tool. But you cannot just pass [] into unique as second argument. I modified it for my own use, but I'd encourage the original author to update it instead of having me posting a minor modification of your work. :)

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!