Matlab onehot到整数

Her*_*lén 1 indexing matlab machine-learning one-hot-encoding

我想在MATLAB中将onehot数组转换为整数值数组.鉴于:

Y =  1     0     0
     0     1     0
     0     1     0
Run Code Online (Sandbox Code Playgroud)

我想回来:

new_y = 1
        2
        2
Run Code Online (Sandbox Code Playgroud)

Wol*_*fie 5

你可以find像这样只使用和返回列索引

Y = [1 0 0; 0 1 0; 0 1 0];

[~, new_y] = find(Y);   % output: [1; 2; 2] is the col indices of your 1s
Run Code Online (Sandbox Code Playgroud)

类似地,如果您的输入是转置,则可以返回行索引

[new_y, ~] = find(Y);   % output: [1; 2; 3] is the row indices of your 1s
Run Code Online (Sandbox Code Playgroud)