How do I create a series of permutation matrices

Hi all,
I'm new, and my maths isn't great. Was wondering if anyone could help me..
I want to create a series of matrices (zeros(3,3)) that show all possible combinations of making 1-6 of the 9 values a 1.
Mathematically, this should produce; 9nCr1 = 9 + 9nCr2 = 36 + 9nCr3 = 84 + 9nCr4 = 126 + 9nCr5 = 126 + 9nCr6 = 84 = 465 separate matrices
Does anyone know how to do that without manually specifying all of them?

 Accepted Answer

a = dec2bin(0:511,9) - '0';
n = sum(a,2);
out = reshape(a(n <= 6 & n >= 1,:).',3,3,[]);
ADD
a = dec2bin(0:511,9) - '0';
n = sum(a,2);
out = reshape(a(n <= 6 & n >= 1 & all(a(:,[1:4:9])==0,2),:).',3,3,[]);

5 Comments

Andrei Bobrov, you are a hero!
Sorry for a follow up - but now I want to remove any matrices where (1,1,:) + (2,2,:) + (3,3,:) > 0.
I have tried the following but it doesn't work;
a = dec2bin(0:511,9) - '0';
n = sum(a,2);
out = reshape(a(n <= 6 & n >= 1,:).',3,3,[]);
for j = 1:length(out)
if (out(1,1,j) + out(2,2,j) + out(3,3,j)) > 0
out(:,:,j) = [];
end
end
Josh
+1 I would have never understood what Josh meant.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!