MATLAB code for an array consisting of matrices

185 Views Asked by At

I want to write MATLAB code that includes an array consisting of all $2 \times 2$ square matrices whose elements are elements of $\Bbb{Z}_2= \{0,1\}$, e.g.,

$$ \begin{pmatrix} 0 & 0 \\ 0 & 0 \end{pmatrix},\quad \begin{pmatrix} 1 & 0 \\ 0 & 0 \end{pmatrix},\quad \begin{pmatrix} 0 & 1 \\ 0 & 0 \end{pmatrix},\quad \begin{pmatrix} 0 & 0 \\ 1 & 0 \end{pmatrix},\quad \begin{pmatrix} 0 & 0 \\ 0 & 1 \end{pmatrix},\quad \begin{pmatrix} 1 & 1 \\ 0 & 0 \end{pmatrix},\quad \cdots$$

And how can I generalize the same thing with $\Bbb{Z}_2$ for $n \times n$ square matrix? Please help!

2

There are 2 best solutions below

1
On

The MATLAB code below generates a $50 \times 50$ array in which each element is a random $100 \times 100$ square binary matrix. You can just change the parameters $n,I$ and $J$ as you wish.

n = 100;
I = 50;
J = 50;
M = [];
for j=1:J
    for i=1:I
       M{i,j} = round(rand(n,n));
    end
end
2
On

You can start with

n = 2    
M = dec2bin(0:2^(n*n)-1,n*n)

then convert strings to matrices.