How can I do one-hot encoding in MATLAB?
Ältere Kommentare anzeigen
I would like to perform one-hot encoding on the vector [1 7 10 9 8 6]' with 10 classes (numbers 1 to 10). The resulting 6x10 matrix should have a 1x10 vector in place of each number, with "1" at the position corresponding to the number and "0" at all other positions.
Akzeptierte Antwort
Weitere Antworten (1)
Jon Cherrie
am 13 Feb. 2026 um 10:04
The function onhotencode will do this for you, e.g.,
x = [1 7 10 9 8 6]' ;
classes = 1:10;
y = onehotencode(x,2,"double",ClassNames=classes)
For more info on this function, see https://www.mathworks.com/help/deeplearning/ref/onehotencode.html
You can use onehotdecode for the reverse operation, which will also work on probability vectors, not just vectors of 1s and 0s
Kategorien
Mehr zu Annotations finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!