Matlab onehot到整数
我想在MATLAB中将onehot数组转换为整数值数组。鉴于:Matlab onehot到整数,matlab,machine-learning,indexing,one-hot-encoding,Matlab,Machine Learning,Indexing,One Hot Encoding,我想在MATLAB中将onehot数组转换为整数值数组。鉴于: Y = 1 0 0 0 1 0 0 1 0 我想返回: new_y = 1 2 2 您可以使用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
Y = 1 0 0
0 1 0
0 1 0
我想返回:
new_y = 1
2
2
您可以使用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
同样,如果输入是转置,则可以返回行索引
[new_y, ~] = find(Y); % output: [1; 2; 3] is the row indices of your 1s
您可以使用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
同样,如果输入是转置,则可以返回行索引
[new_y, ~] = find(Y); % output: [1; 2; 3] is the row indices of your 1s
MATLAB的神经网络工具箱具有用于在一个热向量和指数之间转换的内置功能:创建一个热矩阵,并将一个热矩阵转换回指数向量 注意:ind2vec返回一个稀疏矩阵。要将其转换为完整矩阵,必须使用完整函数
>> Y = full(ind2vec([1, 2, 3]))
Y =
1 0 0
0 1 0
0 0 1
>> new_y = vec2ind(Y)
new_y =
1 2 3
MATLAB的神经网络工具箱具有用于在一个热向量和指数之间转换的内置功能:创建一个热矩阵,并将一个热矩阵转换回指数向量 注意:ind2vec返回一个稀疏矩阵。要将其转换为完整矩阵,必须使用完整函数
>> Y = full(ind2vec([1, 2, 3]))
Y =
1 0 0
0 1 0
0 0 1
>> new_y = vec2ind(Y)
new_y =
1 2 3
我找到了一种方法。也许只有一种选择?我找到了一种方法。也许只有一种选择?[~,new\u y]=maxY,new\u y=new\u y'@Herman谢谢你的问题编辑,让你想要的行为不那么模棱两可,我已经相应地更新了我的答案。谢谢你的回答。我感谢您的反馈@赫尔曼:谢谢你的问题编辑,让你想要的行为不那么模棱两可,我已经相应地更新了我的答案。谢谢你的回答。我感谢您的反馈!