MATLAB: Generating all unique permutations of a matrix with a defined set of arrays

MATLABmatricespermsuniqueunique permutations

I'm trying to generate all the unique permutations of an 8×3 matrix that is created when each row is one of the following:
[1,0,0] [0,1,0] or [0,0,1]
For example, one output would be:
ans =
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
Another output would be:
ans =
0 1 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
1 0 0
And so on…
The following would not be accepted, because the third row [1,1,0] does not match one of the three input arrays.
ans =
0 0 1
0 1 0
1 1 0
0 1 0
1 0 0
0 0 1
1 0 0
0 0 1
Ideally, this can be run in a for loop, so I don't have to store all of the permutations at once. However, I'd also like to know if there's an effective way to store this many matrices (perhaps a 3D matrix), so they can be referenced later.
Thank you,
Peter

Best Answer

Try this. It create a 3D array, where each slice in third dimension is an 8x3 matrix.
A = {[1, 0, 0], [0, 1, 0], [0, 0, 1]};
x = repmat({[1 2 3]}, 1, 8);
combs = combvec(x{:}).';
M = A(combs);
M = cell2mat(M).';
M = reshape(M, 3, 8, []);
M = permute(M, [2 1 3]);