4

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!

Grazel
  • 501
  • 5
  • 17

2 Answers2

5

You can start with

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

then convert strings to matrices.

trang1618
  • 1,565
3

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
Alex Silva
  • 3,557