MATLAB: Create a randomized grouping matrix using one-liner

block designrandomized grouping matrixrandomized matrix

I'm trying to generate a randomized n x m, n>m, matrix that has a single 1 in a random column and 0s elsewhere for every row. It's easy with a for loop:
M = 10;
n = 150;
subj = zeros(n*M,M);
for subj_i = 1:n*M
subj(subj_i,randi(M)) = 1
end
But I'm trying to find a one-liner, here's my attempt:
subj = zeros(n*M,M);
subj(1:n*M,randi(M,1,n*M)) = 1
But it keeps generating all ones everywhere. It'd be great to not even need the initial zeros declaration and to have the option of each column having the same total.
UPDATE:
I figured out how to do it using the Statistics and Machine Learning toolbox, but it'd be good to figure out a way without it, and, again, some way to have equal group sizes would be nice:
dummyvar(randi(M,1,n*M))

Best Answer

Without the constraints, and without initialization,
>> R=[1:N].'; C=randi(M,N,1); % N,M num rows, columns respectively
>> accumarray([R C],ones(N,1))
ans =
0 1 0
0 0 1
1 0 0
0 0 1
0 1 0
>>
Runs the risk if N,M small can always not have every column in every realization of C (then again, it is a random permutation you say).
W/ initialization can ensure output will be full rank of NxM with
>> A=zeros(N,M);
>> A(sub2ind([N,M],R,C))=1
A =
0 1 0
0 0 1
1 0 0
0 0 1
0 1 0
>>