How can I do one-hot encoding in MATLAB?
조회 수: 14 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2020년 4월 29일
편집: MathWorks Support Team
2024년 9월 13일
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.
채택된 답변
MathWorks Support Team
2024년 9월 13일
편집: MathWorks Support Team
2024년 9월 13일
This can be done by using a logical operator '==' as follows:
>> vec = [1 7 10 9 8 6]'; % Create the vector>> A = (vec==1:10) % Find the column that corresponds to each vector entry
A = 6×10 logical array
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0
You can learn more about the operator here:
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!