Question

I'm setting up a script and I want it to systematically go through ALL POSSIBLE 2x2, 3x3, and 4x4 matrices modulo 2, 3, 4, 5, 6, and 7. For example, for modulus 2 in a 2x2, there would be 16 possibilities (4^2 because there are 4 positions with 2 possibilities each). I'm having trouble getting MATLAB to not only form all these possibilities but to put them through my script one at a time. Any thoughts?

Thanks!

Was it helpful?

Solution

This solution uses allcomb from matlab file exchange.

%size
n=2
%maximum value
m=2
%generate input for allcomb
e=cell(1,n^2)
e(1:end)={[0:m-1]}
%generate all combinations.
F=reshape(allcomb(e{:}),[],n,n)

F is a 3D-Matrix, to get the first possibility use:

squeeze(F(1,:,:))

OTHER TIPS

A slight generalization of this Q&A does the job in one line:

r = 2; %// number of rows
c = 2; %// number of columns
n = 2; %// considered values: 0, 1, ..., n-1

M = reshape(dec2base(0:n^(r*c)-1, n).' - '0', r,c,[]);

Result for r, c, n as above:

M(:,:,1) =

     0     0
     0     0

M(:,:,2) =

     0     0
     0     1

...

M(:,:,16) =

     1     1
     1     1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top