MATLAB - constructing a random matrix with specific determinant from its submatrices

133 Views Asked by At

What Matlab code could be used to generate a random matrix whose $2 \times 2$ submatrices obtained from diagonal and anti-diagonal entries give a determinant of either $+1$ or $ -1$? The needed random matrix is an $n\times n$ but we should consider when constructing such matrix that its $2 \times 2$ submatrices obtained from diagonal and anti-diagonal entries give a determinant of either $+1$ or $ -1$. Please see the picture below to understand an example of such a matrix.

enter image description here

Thank you.

1

There are 1 best solutions below

6
On BEST ANSWER

The way I see it.

Let $k$ be the number of diagonal "submatrices" ($k=4$ in your example). It means that the dimensions of your big matrix are $2k \times 2k$ or $(2k+1) \times (2k+1)$.

  • First select a range of variation for your entries, say $[-r,r]$.

  • Then generate an exhaustive list $L$ of all possible $2 \times 2$ matrices $M$ with entries in $[-r,r]$ and $\det(M)=\pm 1$. Let $\ell$ be the length of list $L$.

  • Then generate $k$ times ($k$ has been defined at the beginning) a random integer number $p \in [1,\ell]$, allowing to select in list $L$ (with possible repetition(s)), $k$ matrices $M$ that remain to be "installed" in your big matrix.

Remarks :

  1. Matrix list $L$ could be implemented in Matlab by considering a 3-dimensional array, but it would be awkward. It is better to build this list by concatenating them in this way

$$L=[L,M]$$

(with initialization "void list" $L=[ \ ]$) ending up in a $2 \times (2 \ell)$ array to which you will get access by steps of 2 units.

  1. Obtaining all $2 \times 2$ matrices with determinant $\pm 1$ can be done by brute force (testing/eliminating) is $r$ is moderate.

These two options are taken in the following Matlab program :

   clear all;close all;hold on
   k=4;n=2*k+1;
   r=5;
   ri=@()(floor((2*r+1)*rand-r)); % rand int in [-r,r]
   L=[];
   for s=-r:r
      for t=-r:r
         for u=-r:r
            if (abs(s)>0) && (abs(t)>0) && (abs(u)>0)
               v=(1+t*u)/s;
               if (floor(v)==v) && (abs(v)<(r+1)) && (abs(v)>0);
                L=[L,[s,u;t,v]];end;
               v=(-1+t*u)/s;
               if floor(v)==v && abs(v)<(r+1) && (abs(v)>0);
                L=[L,[s,u;t,v]];end;
            end;
         end;
      end;
   end;
   nm=size(L,2)/2;% number of 2x2 matrices with det = 1 or -1
   % sanity check :
   %D=[];
   %for k=1:2:2*nm
   %  D=[D,det(L(:,k:k+1))];
   %end;
   %hist(D)
   %Initialization
   for p=1:n
      for q=1:n
         M(p,q)=ri();
      end;
   end;
   rng('shuffle');
   for i=1:4;
      r=ceil(nm*rand()+1);
      E=L(:,(2*r-1):(2*r));
      M(i,i)=E(1,1);
      M(i,n+1-i)=E(1,2);
      M(n+1-i,i)=E(2,1);
      M(n+1-i,n+1-i)=E(2,2);
   end;
   M