diagonal

Set diagonal values in a Dataframe

Set diagonal values in a Dataframe Question: Similar questions have been asked already but I still can’t come up with a solution. I have a pandas Dataframe of such a shape: 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 -1 0 0 0 …

Total answers: 1

How to diagonally populate a matrix using numpy diagonal

How to diagonally populate a matrix using numpy diagonal Question: Suppose I have: arr1 = np.array([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20], [21,22,23,24,25]]) And the empty matrix: matrix = np.zeros((10, 10)) matrix[:] = np.NaN I want to populate matrix with each element within arr1, but diagonally. This is the expected output: array([[ nan, nan, nan, nan, nan, nan, …

Total answers: 2

Diagonal difference in Python

Diagonal difference in Python Question: I am working on a HackerRank problem and I don’t understand some of the logic: if (i == j): left += a[i][j] The above is saying if the row/column indices are the same([1,1], [2,2], [3,3]), append value found at those coordinates to the list ‘left’ I don’t understand the logic …

Total answers: 4

Numpy change diagonal values in a 3D array

Numpy change diagonal values in a 3D array Question: I have a vector with the following values: dv = array([0., 0., 1.]). How do I diagonalize this vector into a 3D array with each element in the vector have its own diagonal: array([[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., …

Total answers: 1

Python: Non diagonal elements of a matrix to 0

Python: Non diagonal elements of a matrix to 0 Question: What is the quickest way to convert the non-diagonal elements of a square symmetrical numpy ndarray to 0? Asked By: Prgmr || Source Answers: I’d check out the speed of saving the diagonal away, then zap the matrix, then restore the diagonal: n = len(mat) …

Total answers: 3

NumPy k-th diagonal indices

NumPy k-th diagonal indices Question: I’d like to do arithmetics with k-th diagonal of a numpy.array. I need those indices. For example, something like: >>> a = numpy.eye(2) >>> a[numpy.diag_indices(a, k=-1)] = 5 >>> a array([[ 1., 0.], [ 5., 1.]]) Unfortunately, diag_indices only returns the indices comprising the main diagonal, so at the moment …

Total answers: 6

Get all the diagonals in a matrix/list of lists in Python

Get all the diagonals in a matrix/list of lists in Python Question: I’m looking for a Pythonic way to get all the diagonals of a (square) matrix, represented as a list of lists. Suppose I have the following matrix: matrix = [[-2, 5, 3, 2], [ 9, -6, 5, 1], [ 3, 2, 7, 3], …

Total answers: 14