grp2idx
Create index vector from grouping variable
Description
Examples
Create Index Vector from Categorical Vector
Create a categorical vector by using discretize
and convert it to an index vector by using grp2idx
.
Load the hospital
data set and convert the ages in hospital.Ages
to categorical values representing the ages by decade.
load hospital edges = 0:10:100; % Bin edges labels = strcat(num2str((0:10:90)','%d'),{'s'}); % Labels for the bins s = discretize(hospital.Age,edges,'Categorical',labels);
Display the ages and the groups of ages for the first five samples.
ages = hospital.Age(1:5)
ages = 5×1
38
43
38
40
49
groups = s(1:5)
groups = 5x1 categorical
30s
40s
30s
40s
40s
Create an index vector from the categorical vector s
.
[g,gN,gL] = grp2idx(s);
Display the index values corresponding to the first five samples.
g(1:5)
ans = 5×1
4
5
4
5
5
Reproduce the input argument s
using the output gL
.
gL(g(1:5))
ans = 5x1 categorical
30s
40s
30s
40s
40s
Use gN(g)
to reproduce the input argument s
as a cell array of character vectors.
gN(g(1:5))
ans = 5x1 cell
{'30s'}
{'40s'}
{'30s'}
{'40s'}
{'40s'}
Input Arguments
s
— Grouping variable
categorical vector | numeric vector | logical vector | datetime
vector | duration vector | string array | cell array of character vectors | character array
Grouping variable, specified as a categorical, numeric, logical,
datetime
, or duration vector, a string array, a cell
array of character vectors, or a character array with each row representing
a group label.
grp2idx
treats NaN
s (numeric,
duration, or logical), ''
(empty character arrays or cell
arrays of character vectors), ""
(empty strings),
<missing>
values (string),
<undefined>
values (categorical), and
NaT
s (datetime
) in
s
as missing values and returns
NaN
s in the corresponding rows of
g
. The outputs gN
and
gL
do not include entries for missing
values.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| cell
| categorical
| datetime
| duration
Output Arguments
g
— Group index
positive integer vector
Group index, returned as a positive integer vector with values from 1 up
to the number K of distinct groups in
s
.
gN
— List of group names
cell array of character vectors
List of group names, returned as a cell array of character vectors.
The order of gN
depends on the data type of the
grouping variable s
.
For numeric and logical vectors, the order is the sorted order of
s
.For categorical vectors, the order is the order of
categories(s)
.For other data types, the order is the order of first appearance in
s
.
gN(g)
reproduces the contents of s
in a cell array.
gL
— List of group levels
categorical vector | numeric vector | logical vector | datetime
vector | duration vector | cell array of character vectors | character array
List of group levels, returned as the same data type as
s
: a categorical, numeric, logical,
datetime
, or duration vector, a cell array of
character vectors, or a character array with each row representing a group
label. (The software treats string arrays as cell arrays of character
vectors.)
The set of groups and their order in gL
are the same
as those in gN
, but gL
has the
same data type as s
.
If s
is a character matrix, then
gL(g,:)
reproduces s
; otherwise,
gL(g)
reproduces s
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
The input argument
s
can be a numeric, logical, or character vector or a cell array of character vectors. Code generation does not support a categorical,datetime
, or duration vector or a string array for the input argument.In the generated code, the second and third outputs,
gN
andgL
, are identical.gN
andgL
have the same data type as the input arguments
.
For more information on code generation, see Introduction to Code Generation and General Code Generation Workflow.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)