y
is a column vector with integer values denote label number (say between 1-3).
Now I need to create a matrix with same number of row of y
. Each row i
is now a vector with the y(i)
positioned element to be 1
and the others to be 0
.
Example:
y = [3, 2, 2]';
x = [0,0,1;
0,1,0;
0,1,0];
Is there an efficient way to finish this job. I know a for loop will do:
x = zeros(size(y, 1), 3)
for i = 1:size(y, 1)
x(i, y(i)) = 1
end