%% Encode and Decode Labels
% Use the onehotencode  and onehotdecode functions to encode a set of labels into probability vectors and decode them back into labels.
% Create a vector of categorical labels.

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

% Determine the classes in the categorical vector.

classes = categories(colorsOriginal);

% One-hot encode the labels into probability vectors by using the onehotencode function. Encode the probability vectors into the first dimension.

colorsEncoded = onehotencode(colorsOriginal,1)

% Use onehotdecode to decode the probability vectors.

colorsDecoded = onehotdecode(colorsEncoded,classes,1)

% The decoded labels match the original labels.
% Copyright 2020 The MathWorks, Inc.