MATLAB: How to find all possible combinations for different location settings

enumerate all combinations

There are four locations. Location 1 has two options (1 or 0), location 2 has three options (1, -1, 0), location 3 has one option (0), location four has two options (-1, 0). Totally, there will be 12 possibilities (2*3*1*2=12) considering all different option combinations for these four locations. My question is how to find these 12 possibilities that are stored in a 12*4 matrix?
Thanks

Best Answer

% Locations
loc{1} = [1 0] ;
loc{2} = [1 -1 0] ;
loc{3} = 0 ;
loc{4} = [-1 0] ;
%
N = 12 ;
iwant = zeros(N,4) ;
for i = 1:N
% pick first location
idx_loc1 = randsample(1:2,1) ;
% pick second location
idx_loc2 = randsample(1:3,1) ;
% pick fourth location
idx_loc4 = randsample(1:2,1) ;
iwant(i,:) = [loc{1}(idx_loc1) loc{2}(idx_loc2) loc{3} loc{4}(idx_loc4)] ;
end
iwant