Suppose I have an array a of bounded integers (in this case bounded by 5):
a = [3 4 4 2 1 5 5];
I want to convert this array of integers to a length(a) x 5 matrix A where each row is a bit array with a 1 in the column indexed by the integer from a:
A = [0 0 1 0 0;
0 0 0 1 0;
0 0 0 1 0;
0 1 0 0 0;
1 0 0 0 0;
0 0 0 0 1;
0 0 0 0 1];
This is easily accomplished with a for loop:
n = length(a)
A = zeros(n, max(a(:)));
for k = 1 : n
A(k, a(k)) = 1;
end
I am looking for a vectorized implementation that does not use a for loop.