Main Content

numunique

Number of unique values

Since R2025a

Description

n = numunique(A) returns the number of unique values in A. If A is a table, numunique behaves as if you called numunique(A,"rows").

example

n = numunique(A,"rows") treats each row of A as a single entity and returns the number of unique rows in A.

The "rows" option does not support cell arrays.

example

Examples

collapse all

Create a vector with repeated values. Then, determine the number of unique values in the vector.

A = [9; 2; 9; 9; 5; 2];
n = numunique(A)
n = 
3

Define a vector containing missing values. Then, determine the number of unique values in the vector. numunique treats each instance of a missing value as a distinct value.

A = [5; 8; NaN; NaN];
n = numunique(A)
n = 
4

Create a string array, where one of the strings has trailing white space. Then, determine the number of unique values in the array. numunique treats the string with trailing white space as a distinct string.

A = ["dog" "cat" "horse" "dog "];
n = numunique(A)
n = 
4

Create a matrix with some repeated values. Then, determine the number of unique values in the matrix.

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

     1     2
     3     4
     1     2
     1     2
     3     4

nVal = numunique(A)
nVal = 
4

Determine the number of unique rows in the matrix.

nRow = numunique(A,"rows")
nRow = 
2

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 the number of unique rows in the table. For tabular data, numunique always behaves as if you specify the "rows" option, and it ignores row names.

n = numunique(T)
n = 
3

Input Arguments

collapse all

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

  • If A is a table, then numunique 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 numunique 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.

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

Tips

  • To determine the number of times each unique value appears in the input data, use the groupcounts function.

Version History

Introduced in R2025a