Make a 1 hot vector in MATLAB
Make a vector of length N that consists of all zeros except at index k, where it has the value 1.
Example:
Input N = 6; k = 2; vector = one_hot(N,k);
Output vector = [0 1 0 0 0 0]
Example:
Input N = 6; k = 2; vector = one_hot(N,k);
Output vector = [0 1 0 0 0 0]
Code:
function Y=exi(N,k)
Y=[];
for i=1:N
if(i<k)
Y=[Y 0];
elseif(i==k)
Y=[Y 1];
else
Y=[Y 0];
end
end
end
Explanation:
Output:
No comments