def sparse_to_matrix ( sparse ) : sparse = np . asanyarray ( sparse , dtype = np . int ) if not util . is_shape ( sparse , ( - 1 , 3 ) ) : raise ValueError ( 'sparse must be (n,3)!' ) shape = sparse . max ( axis = 0 ) + 1 matrix = np . zeros ( np . product ( shape ) , dtype = np . bool ) multiplier = np . array ( [ np . product ( shape [ 1 : ] ) , shape [ 2 ] , 1 ] ) index = ( sparse * multiplier ) . sum ( axis = 1 ) matrix [ index ] = True dense = matrix . reshape ( shape ) return dense
create dense matrix from sparse matrix in python
Take a sparse ( n 3 ) list of integer indexes of filled cells turn it into a dense ( m o p ) matrix .
def sparse_to_matrix(sparse):
    """
    Take a sparse (n,3) list of integer indexes of filled cells,
    turn it into a dense (m,o,p) matrix.

    Parameters
    -----------
    sparse: (n,3) int, index of filled cells

    Returns
    ------------
    dense: (m,o,p) bool, matrix of filled cells
    """

    sparse = np.asanyarray(sparse, dtype=np.int)
    if not util.is_shape(sparse, (-1, 3)):
        raise ValueError('sparse must be (n,3)!')

    shape = sparse.max(axis=0) + 1
    matrix = np.zeros(np.product(shape), dtype=np.bool)
    multiplier = np.array([np.product(shape[1:]), shape[2], 1])

    index = (sparse * multiplier).sum(axis=1)
    matrix[index] = True

    dense = matrix.reshape(shape)
    return dense