Main Content

allunique

Determine if all values are unique

Since R2025a

Description

tf = allunique(A) returns logical 1 (true) if all values in A are unique. Otherwise, allunique returns logical 0 (false). If A is a table, allunique behaves as if you called allunique(A,"rows").

example

tf = allunique(A,"rows") treats each row of A as a single entity and returns logical 1 (true) if every row is unique.

The "rows" option does not support cell arrays.

example

Examples

collapse all

Create a vector with repeated values. Then, determine if all elements in the vector have a unique value.

A = [9; 2; 9; 9; 5; 2];
tf = allunique(A)
tf = logical
   0

Create a vector containing missing values. Then, determine if all elements in the vector have a unique value. allunique treats each instance of a missing value as a distinct value.

A = [5; 8; NaN; NaN];
tf = allunique(A)
tf = logical
   1

Create a string array, where one of the strings has trailing white space. Then, determine if all elements in the array have a unique value. allunique treats the string with trailing white space as a distinct string.

A = ["dog" "cat" "horse" "dog "];
tf = allunique(A)
tf = logical
   1

Create a matrix with some repeated values. Then, determine if all values in the matrix are unique.

A = [1 2; 2 1; 3 4; 4 3]
A = 4×2

     1     2
     2     1
     3     4
     4     3

tfVal = allunique(A)
tfVal = logical
   0

Determine if all rows in the matrix are unique.

tfRow = allunique(A,"rows")
tfRow = logical
   1

Create a table with some repeated rows.

A = [38; 43; 38; 40];
B = logical([1; 0; 1; 1]);
T = table(A,B,RowNames=["Smith" "Johnson" "Williams" "Jones"])
T=4×2 table
                A       B  
                __    _____

    Smith       38    true 
    Johnson     43    false
    Williams    38    true 
    Jones       40    true 

Determine if all rows in the table are unique. For tabular data, allunique always behaves as if you specified the "rows" option, and it ignores row names. So, the first and third rows are considered repeated rows.

tf = allunique(T)
tf = logical
   0

Input Arguments

collapse all

Input data, specified as an array, table, or timetable.

  • If A is a table, then allunique does not take row names into account. Two rows that have the same values but different names are considered equal.

  • If A is a timetable, then allunique takes row times into account. Two rows that have the same values but different times are not considered equal.

  • If A is a categorical array, then the sort order is determined by the order of the categories. To see the sort order of a categorical array, use the categories function.

A can also be an object with the class method unique or these class methods:

  • sort (or sortrows, if you specify the "rows" option)

  • ne (not equal)

The methods must not have conflicting behaviors or outcomes. sort or sortrows must use a stable sorting algorithm. For example, you can specify A as a heterogeneous array derived from a common root class, such as an array of graphics objects.

allunique treats missing values and strings with trailing white space as unique values.

Version History

Introduced in R2025a