2
$\begingroup$

I wanted to create a list of matrices from the following rules:

i- All matrices of the list have diagonal equal to zero.

ii- The first matrix have 2x2 dimension, m2={{0,1},{0,0}}

iii- Draw RandomInteger[{1,2}]; 2 is dimension of the matrix start

iv- The next matrix (m3) have 3x3 dimensions and the submatrix have same elements of the matrix write in step ii; the other elements are obtained by RandomNumber (step iii) and elments of the step (ii): The elements of the last Column are: m3[[1,3]]=m2[[1,2]], m3[[2,3]]=m2[[2,2]]. The elements of the last line are m3[[3,1]]=m3[[1,1]], m3[[3,2]]=m3[[1,2]].

v- The next matrix have dimension 4x4; repeat the same elements of the step iv and draw a new RandomInteger[{1,3}] (3 is the dimension of the matrix in step iv). The other elements are obtnained from the matrix m3 and RandomInteger.

For instance: look the figure below

enter image description here. I thought in the code below, but is not working

nmax = 15 ;(*number of matrices*)
m2 = {{0, 1}, {0, 0}} ;(*The matrix begin - start matrix*)
d = 2(*dimension of the matrix begin*);
q = Table[
  RandomInteger[{1, i}], {i, 2, nmax - 1}](*List of Random Integer*)
f[i_, j_] := 
 If[i <= d && j <= d, m2[[i, j]], 
  If[j == i, 0, 
   If[j > i, With[{s = q[[j - d]]}, mat[[i, s]]], 
    With[{p = q[[i - d]]}, mat[[p, j]]]]]](*Rules*)
mat = Array[f, {nmax, nmax}](*write in variable mat the matrix m2*)
c = Table[
  mat[[;; i, ;; i]], {i, d, 
   Length@mat}];(*generate the list of matrix*)
Table[MatrixForm[c[[i]]], {i, 
  nmax - 1}](*Matrix Form of the list matrix*)

Please, somebody help me?

$\endgroup$
  • $\begingroup$ How is this question significantly different from this one that you already asked (148762)? Surely you can modify the answers provided there for this. $\endgroup$ – Edmund Jun 24 '17 at 15:11
  • 1
    $\begingroup$ Also you have asked quite a few questions and have not accepted answers on any of them. Please revisit those questions and accept a suitable answer if one exists. Take The Tour to learn how to do this. $\endgroup$ – Edmund Jun 24 '17 at 15:12
3
$\begingroup$

I would pad the matrix, and then set the last row and column to the right values:

augment[m_, i_] := Module[{new=PadRight[m, Dimensions[m]+1]},
    new[[;;-2, -1]] = m[[All, i]];
    new[[-1, ;;-2]] = m[[i]];
    new
]

Then, you can use FoldList to get the matrices. For your example:

random = {2, 1, 3, 4};
list = FoldList[augment[#1, #2]&, {{0, 1}, {0, 0}}, random];
list //TeXForm

$\left\{\left( \begin{array}{cc} 0 & 1 \\ 0 & 0 \\ \end{array} \right),\left( \begin{array}{ccc} 0 & 1 & 1 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ \end{array} \right),\left( \begin{array}{cccc} 0 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 \\ \end{array} \right),\left( \begin{array}{ccccc} 0 & 1 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 & 0 \\ \end{array} \right),\left( \begin{array}{cccccc} 0 & 1 & 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 0 \\ \end{array} \right)\right\}$

$\endgroup$
  • $\begingroup$ Is working very well. Thank you very much!! $\endgroup$ – SAC Jun 24 '17 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.