Given a set of coordination and values like
<code>coors=np.array([[0,0],[1,1],[2,2]])
heat_values=[3,2,1]
</code>
<code>coors=np.array([[0,0],[1,1],[2,2]])
heat_values=[3,2,1]
</code>
coors=np.array([[0,0],[1,1],[2,2]])
heat_values=[3,2,1]
I would like to generate a matrix like
<code>mtx=[[3,0,0],
[0,2,0],
[0,0,1]]
</code>
<code>mtx=[[3,0,0],
[0,2,0],
[0,0,1]]
</code>
mtx=[[3,0,0],
[0,2,0],
[0,0,1]]
Any functions can do the jobs?
1
I would use numpy.zeros
and indexing:
<code>mtx = np.zeros(coors.max(0)+1, dtype=int)
mtx[tuple(coors.T)] = heat_values
</code>
<code>mtx = np.zeros(coors.max(0)+1, dtype=int)
mtx[tuple(coors.T)] = heat_values
</code>
mtx = np.zeros(coors.max(0)+1, dtype=int)
mtx[tuple(coors.T)] = heat_values
Output:
<code>array([[3, 0, 0],
[0, 2, 0],
[0, 0, 1]])
</code>
<code>array([[3, 0, 0],
[0, 2, 0],
[0, 0, 1]])
</code>
array([[3, 0, 0],
[0, 2, 0],
[0, 0, 1]])
NB. if heat_values
is an array, better use dtype=heat_values.dtype
.
This should generalize to any dimension of coors
:
<code>coors=np.array([[1,0,0],[0,1,1],[0,2,3]])
heat_values=[9,8,7]
mtx = np.zeros(coors.max(0)+1, dtype=int)
mtx[tuple(coors.T)] = heat_values
array([[[0, 0, 0, 0],
[0, 8, 0, 0],
[0, 0, 0, 7]],
[[9, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]])
</code>
<code>coors=np.array([[1,0,0],[0,1,1],[0,2,3]])
heat_values=[9,8,7]
mtx = np.zeros(coors.max(0)+1, dtype=int)
mtx[tuple(coors.T)] = heat_values
array([[[0, 0, 0, 0],
[0, 8, 0, 0],
[0, 0, 0, 7]],
[[9, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]])
</code>
coors=np.array([[1,0,0],[0,1,1],[0,2,3]])
heat_values=[9,8,7]
mtx = np.zeros(coors.max(0)+1, dtype=int)
mtx[tuple(coors.T)] = heat_values
array([[[0, 0, 0, 0],
[0, 8, 0, 0],
[0, 0, 0, 7]],
[[9, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]])
If you want to force a “square” output:
<code>coors=np.array([[0,0],[1,1],[2,3]])
heat_values=[1,2,3]
mtx = np.zeros((np.max(coors)+1,)*coors.shape[1], dtype=int)
mtx[tuple(coors.T)] = heat_values
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]])
</code>
<code>coors=np.array([[0,0],[1,1],[2,3]])
heat_values=[1,2,3]
mtx = np.zeros((np.max(coors)+1,)*coors.shape[1], dtype=int)
mtx[tuple(coors.T)] = heat_values
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]])
</code>
coors=np.array([[0,0],[1,1],[2,3]])
heat_values=[1,2,3]
mtx = np.zeros((np.max(coors)+1,)*coors.shape[1], dtype=int)
mtx[tuple(coors.T)] = heat_values
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]])
Create an array filled with zeros with the appropriate shape, then populate it with the values in the specified coordinates:
<code>coors = np.array([[0, 0], [1, 1], [2, 2]])
heat_values = [3, 2, 1]
mtx = np.zeros((coors[:, 0].max() + 1, coors[:, 1].max() + 1))
mtx[coors[:, 0], coors[:, 1]] = heat_values
</code>
<code>coors = np.array([[0, 0], [1, 1], [2, 2]])
heat_values = [3, 2, 1]
mtx = np.zeros((coors[:, 0].max() + 1, coors[:, 1].max() + 1))
mtx[coors[:, 0], coors[:, 1]] = heat_values
</code>
coors = np.array([[0, 0], [1, 1], [2, 2]])
heat_values = [3, 2, 1]
mtx = np.zeros((coors[:, 0].max() + 1, coors[:, 1].max() + 1))
mtx[coors[:, 0], coors[:, 1]] = heat_values
<code>[[3. 0. 0.]
[0. 2. 0.]
[0. 0. 1.]]
</code>
<code>[[3. 0. 0.]
[0. 2. 0.]
[0. 0. 1.]]
</code>
[[3. 0. 0.]
[0. 2. 0.]
[0. 0. 1.]]
<code>import numpy as np
from scipy.sparse import csr_matrix
# Given coordinates and values
coors = np.array([[0, 0], [1, 1], [2, 2]])
heat_values = [3, 2, 1]
# Determine the size of the matrix
matrix_size = np.max(coors, axis=0) + 1
# Create the CSR matrix
csr_matrix = csr_matrix(
(heat_values, (coors[:, 0], coors[:, 1]) )
, shape=matrix_size)
dense_matrix = csr_matrix.toarray()
'''
[[3 0 0]
[0 2 0]
[0 0 1]]
'''
</code>
<code>import numpy as np
from scipy.sparse import csr_matrix
# Given coordinates and values
coors = np.array([[0, 0], [1, 1], [2, 2]])
heat_values = [3, 2, 1]
# Determine the size of the matrix
matrix_size = np.max(coors, axis=0) + 1
# Create the CSR matrix
csr_matrix = csr_matrix(
(heat_values, (coors[:, 0], coors[:, 1]) )
, shape=matrix_size)
dense_matrix = csr_matrix.toarray()
'''
[[3 0 0]
[0 2 0]
[0 0 1]]
'''
</code>
import numpy as np
from scipy.sparse import csr_matrix
# Given coordinates and values
coors = np.array([[0, 0], [1, 1], [2, 2]])
heat_values = [3, 2, 1]
# Determine the size of the matrix
matrix_size = np.max(coors, axis=0) + 1
# Create the CSR matrix
csr_matrix = csr_matrix(
(heat_values, (coors[:, 0], coors[:, 1]) )
, shape=matrix_size)
dense_matrix = csr_matrix.toarray()
'''
[[3 0 0]
[0 2 0]
[0 0 1]]
'''