Questions tagged [diagonal]

Diagonal means either a diagonal matrix (usually a square matrix) in which the entries outside the main diagonal are all zero. The diagonal entries themselves may or may not be zero. Or, the values of the diagonal matrix.

Since a lot of things in programming is based on matrices doing thing horizontal or vertical is often easier then doing it diagonal. Questions dealing with these difficulties should be tagged diagonal.

Diagonal matrices occur in many areas of linear algebra. Because of the simple description of the matrix operation and eigenvalues/eigenvectors, it is always desirable to represent a given matrix or linear map by a diagonal matrix.

You probably want to add more specific tags about the technology you are using. Areas where the tag ´diagonal´ becomes relevant often drawing and animation.

584 questions
3
votes
5 answers

CSS diagonal lines - how to fit into its parent element?

How can I make a diagonal line fill in and fit into a box (just pure css - without any use of background image)? div.diagonal-container { border: 1px solid #000; width:400px; height:400px; margin: 0…
Run
  • 54,938
  • 169
  • 450
  • 748
3
votes
2 answers

Align dataframe diagonals into columns?

consider the pd.DataFrame df df = pd.DataFrame([ [1, 2, 3, 4, 5], [5, 1, 2, 3, 4], [4, 5, 1, 2, 3], [3, 4, 5, 1, 2], [2, 3, 4, 5, 1] ], list('abcde'), list('ABCDE')) How do I align the diagonal values…
piRSquared
  • 285,575
  • 57
  • 475
  • 624
3
votes
3 answers

Calculate the sum of each diagonal of a matrix

I have a matrix like A and I want to calculate the sum of each diagonal of this matrix and show it in a vector like Y. A=[1 2 3; 4 5 6; 7 8 9] Y=[3 8 15 12 7] I know the code [sum(diag(y,2)),sum(diag(y,1)),sum(diag(y,0)),sum(diag (y,-1)),sum(diag…
3
votes
2 answers

Generating 5x5 matrix given a vector and 0's lined anti-diagonally

Given only the following vector: v <- c(2, 4, 6, 8) The following matrix is desired in which the row-wise directions are alternately right-to-left and left-to-right (by traversing the matrix from top to bottom) and the anti-diagonal is set to…
989
  • 12,579
  • 5
  • 31
  • 53
3
votes
2 answers

Sum of matrix elements between diagonals efficiently in R

I have data in the form of n*n matrix for which I want to do some computations (e.g. sum) on whose elements placed between diagonals (excluding diagonals). For example for this matrix: [,1] [,2] [,3] [,4] [,5] [1,] 2 0 1 4 3 [2,]…
989
  • 12,579
  • 5
  • 31
  • 53
3
votes
2 answers

Diagonals of a multidimensional numpy array

Is there a more pythonic way of doing the following: import numpy as np def diagonal(A): (x,y,y) = A.shape diags = [] for a in A: diags.append(np.diagonal(a)) result = np.vstack(diags) assert result.shape == (x,y) return…
dshin
  • 2,354
  • 19
  • 29
3
votes
2 answers

fill off diagonal of numpy array fails

I'm trying to the fill the offset diagonals of a matrix: loss_matrix = np.zeros((125,125)) np.diagonal(loss_matrix, 3).fill(4) ValueError: assignment destination is read-only Two questions: 1) Without iterating over indexes, how can I set the…
ryan
  • 33
  • 1
  • 4
3
votes
6 answers

What is the fastest way to insert elements diagonally in 2D numpy array?

Suppose we have a 2D numpy array like: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] I want to insert a value say 0 diagonally such that it becomes: matrix = [[0, 1, 2, 3], [4, 0, 5, 6], …
Black Dragon
  • 147
  • 1
  • 2
  • 13
3
votes
1 answer

Main diagonal into anti-diagonal

I need to transform main diagonal {matrix( 1 1 1 1, 0 2 2 2, 0 0 3 3, 0 0 0 4) } into: {matrix( 0 0 0 1, 0 0 1 2, 0 1 2 3, 1 2 3 4) } I tried all operators I could find t(), arev(), flipud(), apply(x,2,rev) and so on. Without a positive result.…
Nils
  • 173
  • 1
  • 1
  • 10
3
votes
4 answers

How to replace the the diagonal elements of a matrix by a vector in SymPy?

I have a vector X which I created like this: from sympy import * x1 = Symbol('x1') x2 = Symbol('x2') x3 = Symbol('x3') X = Matrix([x1, x2, x3]) Then I also have a matrix myMat which just contains ones: myMat = ones(3, 3) Matrix([ [1, 1, 1], [1,…
Cleb
  • 25,102
  • 20
  • 116
  • 151
3
votes
1 answer

Modifying diagonals in multidimensional numpy arrays

I have a multidimensional numpy array of shape (7, 3, 7, 3) and I would like to modify the generalized diagonal in which axis 0 and axis 2 coincide. This generalized diagonal would be defined as those elements of the array whose 0th and 2nd index…
3
votes
2 answers

Calcul of the mean from diagonals of a matrix + incrementation

I've this kind of matrix. I'm really sorry but I don't have a reproducible example. Table 1 : [,1][,2][,3][,4][,5][,6][,7][,8][,9][,10] [1,] 3 NA NA NA NA NA NA NA NA NA [2,] 4 2 NA NA NA NA NA NA NA NA [3,] 4 1…
user3355655
  • 463
  • 3
  • 15
3
votes
2 answers

How to add the diagonals of a matrix in Python 3.3.5

I am using numpy. My assignment is: "Write a function sumOfDiagonal that has one parameter of type list. The list is a 4x4 2-dimensional array of integers (4 rows and 4 columns of integers). The function returns the sum of the integers in the…
AndyM3
  • 183
  • 1
  • 4
  • 13
3
votes
1 answer

rounded diagonal line css

Please tell me how to make such a diagonal line: shape: fiddle link + code: HTML:
CSS: .block { margin: 50px auto; width: 100px; height: 100px; background: black; -webkit-border-radius: 5px; …
user2344095
3
votes
2 answers

Is there a way to flatten a numpy array in diagonal order efficiently?

I am looking for an efficient way (preferably a vectorized fast built-in function) to flatten a numpy array in diagonal order. For example: A=np.array([[1,2,3], [4,5,6], [7,8,9]]) b=flatten_diagonally(A) b should be…
Bitwise
  • 7,577
  • 6
  • 33
  • 50