-1

I want to make an adjacency matrix A given biadjacency matrix B. The type is adjacency matrix in python. Let the matrix B = np.array([[1,2,3],[10,20,30],[3,4,5],[50,12,36],[5,6,7],[3,4,5]]) and its transpose Bt = np.transpose(B). Let, also, two zero matrices Z, Zt with dimensions (6,6) and (3,3) respectively. So A = [[Z,B],[Bt,Zt]]. But this is a list and its elements:

[[array([[ 0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.,  0.]]), array([[ 1,  2,  3],
   [10, 20, 30],
   [ 3,  4,  5],
   [50, 12, 36],
   [ 5,  6,  7],
   [ 3,  4,  5]])], [array([[ 1, 10,  3, 50,  5,  3],
   [ 2, 20,  4, 12,  6,  4],
   [ 3, 30,  5, 36,  7,  5]]), array([[ 0.,  0.,  0.],
   [ 0.,  0.,  0.],
   [ 0.,  0.,  0.]])]].

However I want something like this:

 [0.,  0.,  0.,  0.,  0., 0.,  1,  2,  3
  0.,  0.,  0.,  0.,  0., 0., 10, 20, 30
  0.,  0.,  0.,  0.,  0., 0., 3,  4,  5
  0.,  0.,  0.,  0.,  0., 0., 50, 12, 36
  0.,  0.,  0.,  0.,  0., 0., 5,  6,  7
  0.,  0.,  0.,  0.,  0., 0., 3,  4,  5
  1,   10,  3, 50,  5,  3,  0.,  0.,  0.
  2,   20,  4, 12,  6,  4,  0.,  0.,  0.
  3,   30,  5, 36,  7,  5,  0.,  0.,  0.]

I have tried to work with list comprehensions butthe results are not correct. If I had the original graph I could use NetworX package but I can't now. Do you know a way to construct A?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Angelika
  • 37
  • 6
  • It looks like you want to `flatten` the list. – Carcigenicate Oct 23 '16 at 13:59
  • 1
    read about `.concat`, `.hstack` and `.vstack`, since these are operations you need. Your notation `[[Z,B],[Bt,Zt]]` does not represent your result, your result is after doing "concat_on_top_of_each_other( (Z|B), (Bt|Zt) )" – lejlot Oct 23 '16 at 13:59

2 Answers2

1

What you try to achieve is concatenation, while both your code (and notation in the begining of the question) are about creating multidimensional objects:

>>> X = np.ones((3, 6))
>>> Y = np.ones((6, 3)) * 2
>>> Z1 = np.zeros((6,6))
>>> Z2 = np.zeros((3,3))
>>> 
>>> np.vstack((Z1, X))
array([[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.,  1.]])
>>> np.vstack((Y, Z2))
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
>>> np.hstack((np.vstack((Z1, X)), np.vstack((Y, Z2))))
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  2.,  2.,  2.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  2.,  2.,  2.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  2.,  2.,  2.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  2.,  2.,  2.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  2.,  2.,  2.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  2.,  2.,  2.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.]])
lejlot
  • 64,777
  • 8
  • 131
  • 164
0

The bmat function makes constructing matrices out of other matrices very easy, so

A = np.bmat([[Z, B], [Bt, Zt]]).A

Makes an A that is just like you specified.

chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66