Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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