Skip to content

Instantly share code, notes, and snippets.

@AALEKH
Forked from hunan-rostomyan/label2mat.m
Created August 10, 2020 19:45
Show Gist options
  • Save AALEKH/22767c80ae2015a9c17d60edf792a70f to your computer and use it in GitHub Desktop.
Save AALEKH/22767c80ae2015a9c17d60edf792a70f to your computer and use it in GitHub Desktop.
One Hot Encoder/Decoder in Octave/Matlab
function mat = label2mat(label, size)
if ~exist('size', 'var') || isempty(size)
size = 10;
end
if label > size
error('Label (%d) should be < size (%d).', label, size);
end
I = eye(size);
mat = I(:, label);
end
function label = mat2label(mat)
size = length(mat);
for i = 1:size
if mat(i) == 1
label = i;
break;
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment