I have an m-dimensional vector of integers from 1 to n. These integers are column indices for the m × n matrix.
I want to create an m × n matrix from 0s and 1s, where in the mth row there is 1 in the column that is given by the mth value in my vector.
Example:
% my vector (3-dimensional, values from 1 to 4):
v = [4;
1;
2];
% corresponding 3 × 4 matrix
M = [0 0 0 1;
1 0 0 0;
0 1 0 0];
Is this possible without a for loop?
source
share