Obviously, there are totally $2^{m\times n}$ matrices ($m$ and $n$ is the size of the matrix). I need a command to generate all of them.
Example: for a $1\times 2$ matrix, there are all $4$ possible matrices, $$K=[a_1\ b_1],\ K=[a_1\ b_2],\ K=[a_2\ b_1],\ K=[a_2\ b_2].$$
One way is to write a recursive function:
Then in the command window, call this function (here we use
m=2andn=2as an example):Now
Sis a matrix with2^(m*n)rows andm*ncolumns, all you need is to reshape every row ofSinto anm*nmatrix. (Or you can change the function to directly assign anm*nmatrix toS, this depends on how you want to store the matrices.)Ideas behind the recursion: Since every matrix is just a binary string of length
m*n, all you need is to add one number ($0$ or $1$) at a time, with a total ofm*ntimes. Therefore this is basically a binary tree structure with depthm*n, which motivates the above recursion.