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
4
votes
1 answer

rowsum for matrix over specified number of columns in R

I'm trying to get the sum of columns in a matrix in R for a certain row. However, I don't want the whole row to be summed but only a specified number of columns i.e. in this case all column above the diagonal. I have tried sum and rowSums function…
Triamus
  • 2,415
  • 5
  • 27
  • 37
4
votes
2 answers

Neighbor index computation for diagonally flattened matrix

I have a 2D matrix stored in a flat buffer along diagonals. For example a 4x4 matrix would have its indexes scattered like so: 0 2 5 9 1 4 8 12 3 7 11 14 6 10 13 15 With this representation, what is the most efficient way to…
Thibaut
  • 2,400
  • 1
  • 16
  • 28
4
votes
2 answers

Assiging elements into the lower triangular part of a matrix

I'm creating a diagonal matrix in MATLAB using eye(3). How can I assign the number "2" only to the elements under the main diagonal?
Shakedk
  • 420
  • 6
  • 15
4
votes
1 answer

How can I get the real size of the (lcd) display diagonal, i.e. if a it is a 17 inch or 19 inch or other?

This is useful for me because I have to map objects with a correct dimension on screen; if I'm using a 19" lcd with 1280x1024 resolution and a normal 96dpi setting then in order to map a correct 1-inch square I have to write a xaml like…
Titianus
  • 53
  • 6
3
votes
2 answers

Summing Diagonal Elements of Matrix in Octave

Given a square matrix M, how can you find the sum of the elements on the diagonal? There must be an easier method than this: sum(sum(diag(diag(M), 0)))
grifaton
  • 3,986
  • 4
  • 30
  • 42
3
votes
4 answers

Replace off-block diagonal elements with 0

I'm working in R and I have a matrix with dimensions 6n x 6n. I would like to replace all elements out of a 6x6 block diagonal matrix with zero. Any suggestions? For example (reporting just a 12x12 sample) # create a sample 12x12 matrix > mat <-…
gcoder
  • 41
  • 6
3
votes
5 answers

Replacing off-diagonal elements with a fixed value

I have a square matrix and now I want to replace all its off-diagonal elements with a fixed value. Some approaches how this can be done are discussed in Replacing non-diagonal elements in a matrix in R (hopefully a better asked this time) For…
Brian Smith
  • 1,200
  • 4
  • 16
3
votes
0 answers

Implementation of an algorithm for simultaneous diagonalization

I am trying to write an implementation of an algorithm for the simultaneous diagonalization of two matrices (which are assumed to be simultaneously diagonalizable). However, the algorithm does not seem to converge. The algorithm is described in SIAM…
schade96
  • 119
  • 1
  • 3
  • 7
3
votes
5 answers

Zero diagonal of a PyTorch tensor?

Is there a simple way to zero the diagonal of a PyTorch tensor? For example I have: tensor([[2.7183, 0.4005, 2.7183, 0.5236], [0.4005, 2.7183, 0.4004, 1.3469], [2.7183, 0.4004, 2.7183, 0.5239], [0.5236, 1.3469, 0.5239,…
gus
  • 71
  • 4
3
votes
2 answers

How do I make a mask of diagonal matrix, but starting from the 2nd column?

So here is what I can get with torch.eye(3,4) now The matrix I get: [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]] Is there any (easy)way to transform it, or make such a mask in this format: The matrix I want: [[0, 1, 0, 0], [0, 0, 1, 0], [0, 0,…
ashered
  • 79
  • 7
3
votes
1 answer

How to keep using values from a list until the diagonal of a matrix is full using itertools

So I am trying to use a smaller list to populate the diagonal of a larger matrix. I thought using the cycle function in itertools would make this an easy task but I can't seem to get it to work. Here is what I tried a = np.zeros((10,10)) b = [1, 2,…
3
votes
2 answers

How to create a symmetric matrix where each row/column is a subset of a known vector

I have a 7*1 vector a = (1:7).'. I want to form a matrix A of size 4*4 from vector a such that the elements of a form the anti-diagonals of matrix A as follows: A = [1 2 3 4; 2 3 4 5; 3 4 5 6; 4 5 6 7] I would like this to work for a…
sn at
  • 61
  • 7
3
votes
2 answers

Fast way to set diagonals of an (M x N x N) matrix? Einsum / n-dimensional fill_diagonal?

I'm trying to write fast, optimized code based on matrices, and have recently discovered einsum as a tool for achieving significant speed-up. Is it possible to use this to set the diagonals of a multidimensional array efficiently, or can it only…
PhysLQ
  • 149
  • 2
  • 9
3
votes
2 answers

How to create diagonal array from existing array in numpy

I am trying to make a diagonal numpy array from: [1,2,3,4,5,6,7,8,9] Expected result: [[ 0, 0, 1, 0, 0], [ 0, 0, 0, 2, 0], [ 0, 0, 0, 0, 3], [ 4, 0, 0, 0, 0], [ 0, 5, 0, 0, 0], [ 0, 0, 6, 0, 0], [ 0, 0, 0, 7, 0], …
delta27
  • 197
  • 1
  • 7
3
votes
1 answer

R -Fill in data gaps with length of 'n' or less along a matrix diagonal

I'm working with some large matrices with values along a diagonal similar to the following. ontrack <- matrix(c( runif(1),NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA, runif(1),NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA, …
Robin
  • 465
  • 5
  • 11