Skip to content

Instantly share code, notes, and snippets.

@hunan-rostomyan
Created December 26, 2016 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hunan-rostomyan/410b902c2faf9eb302c5266902a4ed24 to your computer and use it in GitHub Desktop.
Save hunan-rostomyan/410b902c2faf9eb302c5266902a4ed24 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