%% One-Hot Encode a Vector of Labels
% Encode a categorical vector of class labels into one-hot vectors representing the labels.
% Create a column vector of labels, where each row of the vector represents a single observation. Convert the labels to a categorical array.

labels = ["red"; "blue"; "red"; "green"; "yellow"; "blue"];
labels = categorical(labels);

% View the order of the categories.

categories(labels)

% Encode the labels into one-hot vectors. Expand the labels into vectors in the second dimension to encode the classes.

labels = onehotencode(labels,2)

% Each observation in labels is now a row vector with a 1 in the position corresponding to the category of the class label and 0 in all other positions. The function encodes the labels in the same order as the categories, such that a 1 in position 1 represents the first category in the list, in this case, 'blue'.
% Copyright 2020 The MathWorks, Inc.