numunique - Number of unique values - MATLAB (original) (raw)

Number of unique values

Since R2025a

Syntax

Description

`n` = numunique([A](#mw%5F2e769f1e-d933-43c2-8031-3ffc9bb11310)) returns the number of unique values in A. If A is a table,numunique behaves as if you callednumunique(A,"rows").

example

n = numunique([A](#mw%5F2e769f1e-d933-43c2-8031-3ffc9bb11310),"rows") treats each row ofA as a single entity and returns the number of unique rows inA.

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)

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)

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)

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

Determine the number of unique rows in the matrix.

nRow = numunique(A,"rows")

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.

Input Arguments

collapse all

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

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

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

Version History

Introduced in R2025a