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
3 answers

Matrix to Diagonal Matrix in MATLAB

Let's say I have a matrix in MATLAB like A = [1 2 3; 4 5 6; 7 8 9] and I would like to obtain a matrix of the form B = [1 0 0; 0 4 0; 0 0 7; 2 0 0; 0 5 0; 0 0 8; 3 0 0; 0 6 0; 0 0 9] i.e. a…
Erol
  • 6,478
  • 5
  • 41
  • 55
3
votes
3 answers

Trying to get a diagonal sum

Trying to get the sum diagonally in #7 of the lists that #6 creates. So say the list was this [[2,3,1],[1,1,1],[5,6,4]] the sum would be 2+1+4 #6 def randomlists(s): b=s list1 = [] while s>0: sublist = [] for x in…
Tommy Kay
  • 103
  • 1
  • 3
  • 9
3
votes
3 answers

How to create a diagonal sparse matrix in SciPy

I am trying to create a sparse matrix which has a 2D pattern run down the diagonal. This is probably easiest to explain with a quick example. Say my pattern is: [1,0,2,0,1]... I want to create a sparse matrix: [[2,0,1,0,0,0,0...0], …
user1354372
  • 77
  • 2
  • 7
2
votes
1 answer

MATLAB: Scatterplot - points with different shapes according to position

The properties of my figure I want to build are the following: The figure shows 200 points. The points above the diagonal should be shown with red stars and the one below the diagonal, with blue triangles. This is what I've managed to do so far…
Alex Encore
  • 299
  • 1
  • 13
  • 26
2
votes
1 answer

How to convert operators into Matrices?

I am trying to find the eigenvalues of a Hamiltonian using the Quantumoptics.jl package. But every time I try to do that it gives me an error saying Methoderror: no method matching eigen(::Operator{FockBasis{Int64}, FockBasis{Int64},…
2
votes
1 answer

Pytorch Batchwise block diagonal

I have two tensors containing batches of matrices of the same batch size (first dimension) but different matrix structure (all other dimensions). For example A of shape (n,d,d) and B (n,e,e). Now I would like to build block diagonals of A and B for…
yeich
  • 23
  • 2
2
votes
2 answers

How to diagonally populate a matrix using numpy diagonal

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…
XioHu
  • 123
  • 4
2
votes
2 answers

divide each row with the value in the main diagonal in R

I have a dataset in R as follows. dat <- as.data.frame(t(cbind(c(5,10,15,20),c(10,20,30,40),c(50,100,150,200),c(200,400,600,800)))) dat How can create a new dataframe dat_new that each row has been divided by the value in the main diagonal of…
nickolakis
  • 621
  • 3
  • 7
2
votes
1 answer

How do we make a function that allows us to check if the list of string are consecutive?

Given a board of n x n size that is greater than 4 and no larger than 100. How can we check if a list[str] has connecting elements from left to right, up to down, and diagonally (top left to bottom right / top right to bottom left)? for example if…
Sungchunn
  • 29
  • 5
2
votes
1 answer

How to sort a confusion matrix by the diagonal value

I have this confusion matrix: import pandas as pd import seaborn as sn import matplotlib.pyplot as plt data = {'y_Actual': [3, 3, 1, 1, 0, 1, 2, 3, 1, 1, 1, 0, 2, 4, 3], 'y_Predicted': [1, 2, 2, 1, 0, 1, 3, 0, 1, 0, 0, 0, 3, 4, 2] …
codelifevcd
  • 175
  • 1
  • 10
2
votes
1 answer

More efficient way to get diagonal length per coordinate

I have an array of x and y values (coordinates) representing matches and for each of these x,y's I want to know the length of the diagonal it is part of. For example let's take these coordinates Data description coords = np.asarray([[0,0], [0,7],…
CodeNoob
  • 1,988
  • 1
  • 11
  • 33
2
votes
3 answers

How to replace values in a matrix below the diagonal from bottom left to top right?

Given is a matrix (no assumptions are made about its dimensions) which contains zeros below the diagonal that runs from the bottom left to the upper right corner: m <- matrix(data = c(2, 1, 8, 9, 1, 5, 0, 4, 3, 6, …
PeterD
  • 429
  • 2
  • 13
2
votes
1 answer

How can I set the diagonal of an N-dim tensor to 0 along given dims?

I’m trying to figure out a way to set the diagonal of a 3-dimensional Tensor (along 2 given dims) equal to 0. An example of this would be, let’s say I have a Tensor of shape [N,N,N] and I wanted to set the diagonal along dim=1,2 equal to 0? How…
momo
  • 90
  • 9
2
votes
1 answer

Assign shifted diagonal values with np.diag

I'm trying to assign values to multiple diagonals of a matrix. For example, I have this matrix: >>> u = np.zeros(25).reshape(5, 5) >>> u array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.], [0., 0., 0., 0.,…
Ivan Gonzalez
  • 446
  • 1
  • 5
  • 14
2
votes
1 answer

Tensorflow: How to get the value of the k-th diagonal

In PyTorch, the function torch.diag() gets the value of the k-th diagonal of a tensor. For example, a.diag(diagonal=1) gets the value of the 1-th diagonal. Unfortunately diag_part() doesn't appear to work in Tensorflow: a = [[1, 2, 3], [4, 5,…
liuliang
  • 23
  • 4