1

Is there a way/code to convert the following n x 1 matrix,

x1
x2
x3
x4
x5
x6
x7
x8
x9
x10

etc.

into a square matrix of the form,

x1 x2 x4 x7 
x2 x3 x5 x8
x4 x5 x6 x9
x7 x8 x9 x10

etc.

I have a 903 x 1 matrix (in a .csv format) that I hope to convert into a 42 x 42 matrix with the form as shown. Thanks!

kwaldner
  • 95
  • 1
  • 6
  • Possible duplicate of [How to define a two-dimensional array in Python](https://stackoverflow.com/questions/6667201/how-to-define-a-two-dimensional-array-in-python) – Mihai Chelaru Jun 19 '18 at 02:36

2 Answers2

1

I suppose I should wait until you edit the question, but I went ahead and looked at the figure. It looks like a symmetric matrix based on tri-upper and lower matrices. In what dicispline is that called a `full matrix'?

Anyhow's here one sequence that produces your figure:

In [93]: idx=np.tril_indices(4)
In [94]: idx
Out[94]: (array([0, 1, 1, 2, 2, 2, 3, 3, 3, 3]), array([0, 0, 1, 0, 1, 2, 0, 1, 2, 3]))
In [95]: arr = np.zeros((4,4),int)
In [96]: arr[idx] = np.arange(1,11)
In [97]: arr
Out[97]: 
array([[ 1,  0,  0,  0],
       [ 2,  3,  0,  0],
       [ 4,  5,  6,  0],
       [ 7,  8,  9, 10]])
In [98]: arr1 = arr + arr.T
In [99]: arr1
Out[99]: 
array([[ 2,  2,  4,  7],
       [ 2,  6,  5,  8],
       [ 4,  5, 12,  9],
       [ 7,  8,  9, 20]])
In [100]: dx = np.diag_indices(4)
In [101]: dx
Out[101]: (array([0, 1, 2, 3]), array([0, 1, 2, 3]))
In [102]: arr1[dx] = arr[dx]
In [103]: arr1
Out[103]: 
array([[ 1,  2,  4,  7],
       [ 2,  3,  5,  8],
       [ 4,  5,  6,  9],
       [ 7,  8,  9, 10]])

This is similar to what scipy.spatial calls a squareform for pairwise distances.

https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.spatial.distance.squareform.html#scipy.spatial.distance.squareform

In [106]: from scipy.spatial import distance
In [107]: distance.squareform(np.arange(1,11))
Out[107]: 
array([[ 0,  1,  2,  3,  4],
       [ 1,  0,  5,  6,  7],
       [ 2,  5,  0,  8,  9],
       [ 3,  6,  8,  0, 10],
       [ 4,  7,  9, 10,  0]])

It appears that this square_form uses compiled code, so I expect it will be quite a bit faster than my tril base code. But the order of elements isn't quite what you expect.

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • thanks! is there a more general way to convert an n x 1 matrix into an n x n matrix? I have edited the question :) – kwaldner Jun 19 '18 at 14:30
  • You specified a complex pattern. How general do you expect the code to be? – hpaulj Jun 19 '18 at 14:41
  • Quite general. I have a 903 x 1 matrix (in a .csv format) where each element is a number (for example, 3.56789E-05), and I hope to convert this 903 x 1 matrix into a 42 x 42 matrix with the form as shown in the question. Thanks! – kwaldner Jun 19 '18 at 14:49
  • Replacing the 4s in my code with 42 would be start! – hpaulj Jun 19 '18 at 16:01
0

Numpy has a function to reshape arrays - https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html

>>> np.reshape(a, (2, 3)) # C-like index ordering
array([[0, 1, 2],
       [3, 4, 5]])
Rincewind
  • 1
  • 2