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
6
votes
0 answers

What is the most efficient way to add a Eigen::DiagonalMatrix to an Eigen::SparseMatrix

I am surprised to find out that it's not possible to simply add an Eigen::DiagonalMatrix to an Eigen::SparseMatrix: #include #include int main() { Eigen::Matrix4d M; M<<1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16; …
Alec Jacobson
  • 6,032
  • 5
  • 51
  • 88
6
votes
1 answer

Diagonalize 2d matrix along one axis in numpy

Given the matrix X in dimension of MxN. I want to create a diagonal matrix for every row of X. The result should be in MxNxN. How to do it efficiently? Thank you!
Chris Zhang
  • 113
  • 1
  • 6
6
votes
3 answers

How to draw a perfect diagonal with linear-gradient

I am trying to draw a decent diagonal with linear-gradient but I can't figure out how to do it when the container is small> I am trying to get a diagonal that fits inside a 10x10px container and looks like this: This is my best attempt. div { …
Cain Nuke
  • 2,843
  • 5
  • 42
  • 65
6
votes
2 answers

Matlab: Remove diagonal from matrix and reform

I would like to remove the diagonal of the following matrix; [0 1 1 0 0 0 0 1 0] and put this in a vector as such [1 1 0 0 0 1] Is there a one-way function to do this? Most other solutions I found on Stack Overflow delete all zeros.
Jill
  • 69
  • 3
6
votes
4 answers

Matlab decreasing matrix diagonal

I want to create a matrix where the middle diagonal is symmetrically decreasing to the sides, like this: 5 4 3 2 1 4 5 4 3 2 3 4 5 4 3 2 3 4 5 4 1 2 3 4 5 The matrix has to be 100x100 and the values are between 0 and 1. Until now I only get the…
Benny Müller
  • 185
  • 1
  • 14
6
votes
6 answers

Draw android xml shape diagonal with triangles

Hi I would like to draw something like this with a shape Is it possible ?
Pauli
  • 538
  • 8
  • 22
6
votes
2 answers

Insert number above the diagonal in R matrix

I have a vector like this in R: vec1 <- c(14000,12000,8000) I'm trying to create a matrix where 14000 is my main diagonal, 1200 is one above the diagonal, 8000 two above the diagonal. I'm familiar with doing this in Python/numpy but can't figure it…
itjcms18
  • 3,993
  • 7
  • 26
  • 45
6
votes
3 answers

Numpy: Affect diagonal elements of matrix prior to 1.10

I would like to change diagonal elements from a 2d matrix. These are both main and non-main diagonals. numpy.diagonal() In NumPy 1.10, it will return a read/write view, Writing to the returned array will alter your original…
FooBar
  • 15,724
  • 19
  • 82
  • 171
6
votes
2 answers

In R, convert data frame diagonals to rows

I'm developing a model that forecasts completed fertility for an age cohort. I currently have a data frame like this, where the rows are ages and the columns are years. The value in each cell is age-specific fertility for that year: > df1 iso3 …
rsoren
  • 4,036
  • 3
  • 26
  • 37
6
votes
3 answers

How to repeat a vector along a diagonal in Matlab

I would like to repeat a vector A of length n on a diagonal m times to obtain a (n+m-1) x m matrix B. As an example let's say A = [a;b;c;d], m = 4. This should result in B = [a 0 0 0; b a 0 0; c b a 0; d c b a; 0 d c b; 0 0 d c; 0 0 0 d] Any…
julian
  • 63
  • 5
6
votes
2 answers

Point inside a rectangle - On which side of the diagonal is it?

So, I have a rectangle divided into two triangles and I have a point: I know the coordinates of the four vertices of the rectangle (A, B, C and D) and I know the coordinates of P. What I need to know is in which of the two triangles is the point. I…
Sylar
  • 357
  • 1
  • 5
  • 15
6
votes
5 answers

How to move a visible image diagonally?

I have been trying to figure out how to make a visible image move diagonally in an applet window. If you press up, down, left, or right the image (a gif) moves accordingly, however if you try to press two keys at once (up and right at the same…
Greywarden
  • 63
  • 1
  • 4
5
votes
1 answer

Flexible diagonal line plot in R

How can we plot diagonal across (from bottom-left-hand corner to top right-hand corner), at any given coordinate ranges? For example > plot(c(-2,3), c(-1,5), type = "n", xlab="x", ylab="y", asp = 1) or > plot(c(0,1000), c(0,334), type = "n",…
neversaint
  • 60,904
  • 137
  • 310
  • 477
5
votes
3 answers

Diagonal snake filling array

Python 3.7. I'm trying to fill multidimensional array (n*m size) in diagonal-snake pattern: 1 3 4 10 11 21 2 5 9 12 20 22 6 8 13 19 23 30 7 14 18 24 29 31 15 17 25 28 32 35 16 26 27 33 34 36 I have a function…
Neuron
  • 1,020
  • 2
  • 13
  • 28
5
votes
2 answers

How to create an anti-diagonal identity matrix (where the diagonal is flipped left to right) in numpy

How can I create anti-diagonal matrix in numpy? I can surely do it manually, but curious if there is a function for it. I am looking for a Matrix with the ones going from the bottom left to the upper right and zeros everywhere else.
user1700890
  • 7,144
  • 18
  • 87
  • 183
1 2
3
38 39