Skip to content

Instantly share code, notes, and snippets.

@zygmuntz
Created October 11, 2013 18:31
Show Gist options
  • Save zygmuntz/6939718 to your computer and use it in GitHub Desktop.
Save zygmuntz/6939718 to your computer and use it in GitHub Desktop.
One way of doing one-hot encoding in Matlab
% y is a vector of labels
y_one_hot = zeros( size( y, 1 ), n_classes );
% assuming class labels start from one
for i = 1:n_classes
rows = y == i;
y_one_hot( rows, i ) = 1;
end
@kanginthaya
Copy link

y_one_hot = ind2vec(y')'; // matlab has this built-in function

y_one_hot = full(ind2vec(y',num_classes)) is a clean way to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment