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)) = 1end
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