Easy enough.
matno = 10;
rows=10;
columns=3;
Now we want to create a matrix of size (rows,columns,matno), such that each row has exactly one element that is 1, and the placement of that 1 is random. This is most simply done by creating a matrix of size (matno*rows,columns), where each of those rows has exactly one true element.
The location of those 1's will be:
cloc = randi(columns,[matno*rows,1]);
matrices = zeros(matno*rows,columns);
matrices(sub2ind([matno*rows,columns],(1:(matno*rows))',cloc)) = 1;
matrices = permute(reshape(matrices,matno,rows,columns),[2 3 1]);
matrices
matrices(:,:,1) =
0 0 1
0 0 1
1 0 0
0 1 0
0 1 0
1 0 0
1 0 0
0 0 1
0 0 1
1 0 0
matrices(:,:,2) =
0 1 0
1 0 0
1 0 0
0 0 1
1 0 0
0 0 1
1 0 0
0 1 0
1 0 0
0 0 1
matrices(:,:,3) =
1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
1 0 0
1 0 0
0 1 0
0 0 1
1 0 0
matrices(:,:,4) =
0 1 0
0 0 1
0 1 0
0 1 0
0 0 1
0 0 1
0 1 0
0 1 0
0 1 0
1 0 0
matrices(:,:,5) =
1 0 0
1 0 0
0 1 0
0 0 1
1 0 0
1 0 0
0 0 1
0 1 0
0 1 0
0 1 0
matrices(:,:,6) =
0 0 1
0 0 1
1 0 0
1 0 0
0 0 1
0 1 0
0 1 0
1 0 0
0 0 1
0 1 0
matrices(:,:,7) =
1 0 0
0 0 1
1 0 0
0 1 0
0 0 1
1 0 0
0 0 1
0 0 1
0 1 0
1 0 0
matrices(:,:,8) =
0 0 1
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
1 0 0
0 1 0
0 1 0
0 0 1
matrices(:,:,9) =
0 0 1
1 0 0
0 0 1
1 0 0
1 0 0
0 1 0
0 1 0
0 0 1
0 0 1
1 0 0
matrices(:,:,10) =
1 0 0
1 0 0
1 0 0
0 0 1
0 1 0
0 1 0
1 0 0
0 1 0
1 0 0
0 1 0
Easy. Almost trivial. To prove that the result has exactly one 1 in each row, this next result should be a 10x10 array of 1's.
squeeze(sum(matrices,2))
ans =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
As it is.
Best Answer