0

I am struggling with this problem for some time and i dont find a solution. I try to plot a heatmap and have read some stuff but the problems are different. I have 3 Vectors:

    x = [ -1,  0,  1, -1,  0,  1,  0, -1,  1 ]
    y = [ -1, -1, -1,  0,  0,  0,  1,  1,  1 ]
    E = [  3,  1,  4,  1,  5,  9,  6,  2,  5 ]

What i want is a matrix like the one below for the actual plotting:

E_xy = [ [ 3, 1, 4],
         [ 1, 5, 9],
         [ 2, 6, 5]]

x[0] belongs to y[0] belongs to E[0] and so on.

What is the best/easiest way to do this?

Please note: The ordering of the matrix can not be used (see E[7] and E[8] and the resulting E_xy[2,0] and E_xy[2,1]).

ano302
  • 3
  • 3
  • I'm not quite sure what you mean. Would it be possible to consider rephrasing? All it looks like is the splitting an array of length 9 into three arrays of length 3 while preserving the order. I.e `E_xy = [ E[i:i + 3] for i in [x*len(E)/3 for x in range(3)]]` – Will Nov 09 '17 at 16:37
  • 2
    How *do* you determine the correct ordering if E is not correctly ordered? – Jan Christoph Terasa Nov 09 '17 at 16:42

2 Answers2

0

Assuming a square NxN matrix

import numpy as np

x = np.array([ -1,  0,  1, -1,  0,  1,  0, -1,  1 ])
y = np.array([ -1, -1, -1,  0,  0,  0,  1,  1,  1 ])
E = np.array([  3,  1,  4,  1,  5,  9,  6,  2,  5 ])
N = int(np.sqrt(E.size))

sorting = np.argsort(x + y*N)
E_xy = E[sorting].reshape(N,N)
Jan Christoph Terasa
  • 5,781
  • 24
  • 34
  • This doesn't take the offsets from x and y into account, does it? – jbndlr Nov 09 '17 at 16:40
  • What do you mean by "offsets"? It uses the values in `x` as the low-order (column) index, and the values in `y` as the high-order (row) index. – Jan Christoph Terasa Nov 24 '17 at 07:34
  • You fixed it in your edit by adding `argsort` - before, you did not pay respect to the `x,y` indices. – jbndlr Nov 24 '17 at 09:16
  • 1
    Yeah, it's a bit unfortunate that you can edit answers posts on SO, but the comments stay the same. They should either force user to add a new answer, or mark comments which relate to the previous answer. – Jan Christoph Terasa Nov 25 '17 at 12:11
0

Interpreting your x and y lists as definitions of positional offsets in relation to the matrix' center element, you can first create tuples containing your positions and then fill everything into a newly created matrix of desired dimensions:

x = [ -1,  0,  1, -1,  0,  1,  0, -1,  1 ]
y = [ -1, -1, -1,  0,  0,  0,  1,  1,  1 ]
E = [  3,  1,  4,  1,  5,  9,  6,  2,  5 ]

positions = [
    (x[i] + 1, y[i] + 1, E[i])
    for i in range(len(x))
]

result = [
    [None] * 3 for _ in range(3)
]
for x, y, value in positions:
    result[y][x] = value

print(result)

Note that this is not a general solution; it assumes the matrix to always be 3x3; it requires changes to make this a general approach for arbitrarily shaped matrices.

The above code prints:

>>> [[3, 1, 4], [1, 5, 9], [2, 6, 5]]

Additionally, the above code surely can be written more efficient, but would lose in readability then; so it's up to you to save on memory, if you need to (if matrices are growing large).

jbndlr
  • 4,965
  • 2
  • 21
  • 31