MATLAB: How can I find all the possible binaries matrices from a specifiec size

binarymatrixrecursion

By given a size of a matrix, which contains only 2 numbers, how can I find all the possible matrices from that kind? we can assume that the matrix is squared. For example, if my size is 2*2, I will get the matrices: [0 0; 0 0] [1 0; 0 0] [0 1; 0 0] [0 0;1 0] [0 0;0 1] [1 1; 0 0] [1 0;1 0] [1 0; 0 1] [0 1;1 0] [0 1;0 1] [0 0;1 1] [1 1; 1 0] [1 1; 0 1] [1 0;1 1] [0 1; 1 1] [1 1 ;1 1]

Best Answer

m=2;n=2;
[C{1:m*n}]=ndgrid(logical([0,1]));
C=cellfun(@(x) x(:).', C, 'uni',0);
A=reshape( vertcat(C{:}) , m,n, []),