%% One-Hot Encode Table with Several Variables
% If your data is a table that contains several types of class variables, you can encode each variable separately.
% Create a table of observations of several types of categorical data.

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

pets = ["dog"; "fish"; "cat"; "dog"; "cat"; "bird"];
pets = categorical(pets);

location = ["USA"; "CAN"; "CAN"; "USA"; "AUS"; "USA"];
location = categorical(location);

data = table(color,pets,location)

% Use a for-loop to one-hot encode each table variable and append it to a new table containing the encoded data.

encData = table();

for i=1:width(data)
 encData = [encData onehotencode(data(:,i))];
end

encData

% Each row of encData encodes the three different categorical classes for each observation.
% Copyright 2021 The MathWorks, Inc.