title: 3d matrix to 2d adjacency matrix or edgelist
body: <p>Consider a <strong>3 x 3 x 3</strong> cube where each of the 27 elements is connected to other elements along faces. A cube-shaped element has 6 sides, thus a maximum of 6 connections is possible per element (for example, the center-most element in a 3 x 3 x 3 cube is bounded by 6 elements, and has 6 connections).  </p>
  
  <p>Then, let <code>m1</code>, <code>m2</code>, and <code>m3</code> be the first, second, and third layers of the cube respectively. The name of each element is <code>xyz</code>, where <code>x</code>, <code>y</code>, <code>z</code> are the row number, column number, and layer number of the element. For example, the element <code>213</code> is in the second row, first column, and 3rd layer of the cube. This element is connected to 4 other elements: three are in its layer (<code>113, 313, 223</code>), and one is one layer above it (<code>212</code>).     </p>
  
  <pre><code>x = 3 # nrow
  y = 3 # ncol
  z = 3 # nlay
  
  # print each layer as a 2D matrix
  for(k in 1:z){
    m = paste0(rep(1:x, each=x), rep(1:y, times = y), k)
    print(matrix(m, nrow=x, byrow=T))
  }
  
       [,1]  [,2]  [,3] 
  [1,] "111" "121" "131"
  [2,] "211" "221" "231"
  [3,] "311" "321" "331"
       [,1]  [,2]  [,3] 
  [1,] "112" "122" "132"
  [2,] "212" "222" "232"
  [3,] "312" "322" "332"
       [,1]  [,2]  [,3] 
  [1,] "113" "123" "133"
  [2,] "213" "223" "233"
  [3,] "313" "323" "333"
  </code></pre>
  
  <h3>Is there an out-of-the-box function in <code>igraph</code> or a related package for creating either an <strong>adjacency matrix OR an edge list</strong> for a network like this? I need a solution <strong>that scales to any number of rows, columns, and layers</strong>. Python solutions are welcome.</h3>
  
  <p>I manually created the 2D adjacency matrix, where the rows and columns are given by <code>c(m1, m2, m3)</code> below:  </p>
  
  <pre><code>m1 = paste0(rep(1:x, each=x), rep(1:y, times = y), 1)
  m2 = paste0(rep(1:x, each=x), rep(1:y, times = y), 2)
  m3 = paste0(rep(1:x, each=x), rep(1:y, times = y), 3)
  c(m1, m2, m3)
   [1] "111" "121" "131" "211" "221" "231" "311" "321" "331" "112" "122" "132" "212" "222" "232" "312" "322" "332"
  [19] "113" "123" "133" "213" "223" "233" "313" "323" "333"
  </code></pre>
  
  <p>For this simple example the adjacency matrix is sparse, has 0's along the diagonal, and is symmetric. It looks like this:  </p>
  
  <p><a href="https://i.stack.imgur.com/Ise1g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ise1g.png" alt="enter image description here"></a></p>
  
  <p>And here's a <code>dput()</code> to C&amp;P and validate with.  </p>
  
  <pre><code>dput(temp)
  structure(c(0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 
  1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 
  1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 1, 0,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, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 
  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
  0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0), .Dim = c(27L, 
  27L), .Dimnames = list(c("111", "121", "131", "211", "221", "231", 
  "311", "321", "331", "112", "122", "132", "212", "222", "232", 
  "312", "322", "332", "113", "123", "133", "213", "223", "233", 
  "313", "323", "333"), c("111", "121", "131", "211", "221", "231", 
  "311", "321", "331", "112", "122", "132", "212", "222", "232", 
  "312", "322", "332", "113", "123", "133", "213", "223", "233", 
  "313", "323", "333")))
  </code></pre>