MATLAB: Make a logical matrix from an available array of numbers.

arraylogical matrix

i get an array of numbers in which minimum number is 1 and maximum number is 7 , total elements in that array are 6, e.g.
A=[2 5 6 2 7 4]
I want to make a logical matrix of order 7*6 in such a way that there is only one '1' in each column and all other entries of that column are '0'. Now the numbers in array A indicate the row number at which binary '1' is to be placed. e.g. for above provided array, my logical matrix would be:
b =
0 0 0 0 0 0
1 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 1
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 0 1 0
kindly generalize me this program. for any random array 'A'.
Thank you.

Best Answer

cols = 1:numel(A);
B = zeros(max(A), numel(A));
B(sub2ind(size(B), A, cols)) = 1