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
5
votes
2 answers

Masking diagonal to a specific value with PyTorch tensors

How do I fill the diagonal with a value in torch? In numpy you can do: a = np.zeros((3, 3), int) np.fill_diagonal(a, 5) array([[5, 0, 0], [0, 5, 0], [0, 0, 5]]) I know that torch.diag() returns the diagonal, but how to use this as a…
NicolaiF
  • 1,283
  • 1
  • 20
  • 44
5
votes
3 answers

Construct (N+1)-dimensional diagonal matrix from values in N-dimensional array

I have an N-dimensional array. I want to expand it to an (N+1)-dimensional array by putting the values of the final dimension in the diagonal. For example, using explicit looping: In [197]: M = arange(5*3).reshape(5, 3) In [198]:…
gerrit
  • 24,025
  • 17
  • 97
  • 170
5
votes
2 answers

Replace diagonals of a 2D array with python

I have the following 2D array A=([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]) And I want to replace the main diagonal by the array a = ([0,2,15,20]) Therefore, the results must be A=([[0, 2, 3, 4], [5, 2, 7,…
DiegoDZ
  • 189
  • 1
  • 11
5
votes
2 answers

Build a block diagonal matrix from a known matrix

I want to build a block diagonal matrix (A) from a known matrix (B) by putting B in diagonal positions of A. Let's say my B: > matrix(c(1,3,4,5),nrow=2) [,1] [,2] [1,] 1 4 [2,] 3 5 I am looking for a function like this:…
Square9627
  • 869
  • 3
  • 10
  • 15
5
votes
2 answers

Pythonic way to get both diagonals passing through a matrix entry (i,j)

What is the Pythonic way to get a list of diagonal elements in a matrix passing through entry (i,j)? For e.g., given a matrix like: [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 an entry, say, (1,3)…
user8305079
5
votes
3 answers

Lower triangular matrix in julia

I have the number of columns equals the number of rows. And the the diagonal is is equal to zero. How can I build this matrix? #mat # [,1] [,2] [,3] [,4] #[1,] 0 NA NA NA #[2,] 1 0 NA NA #[3,] 2 4 0 NA #[4,] 3 5…
vincet
  • 917
  • 3
  • 13
  • 26
5
votes
2 answers

Diagonalize symbolic matrix

I need to diagonalize a symbolic matrix with python. In Mathematica it can be done easily, but when using the module numpy.linalg I get problems. For concreteness, consider the matrix [[2, x], [x, 3]] where x is a symbolic variable. I guess I get…
dapias
  • 2,512
  • 3
  • 15
  • 23
5
votes
4 answers

R or MATLAB: permute a large sparse matrix into a block diagonal matrix

I have a large sparse matrix, and I want to permute its rows or columns to turn the original matrix into a block diagonal matrix. Anyone knows which functions in R or MATLAB can do this? Thanks a lot.
user1867277
  • 51
  • 1
  • 2
5
votes
0 answers

Control if diagonal swipe is for horizontal UIScrollView or nested vertical UIScrollView?

Short How can I control wether or not a swipe on a UIScrollView has made a big enough Y delta to be considered a vertical swipe and let it be handled by a nested UIScrollView (UIWebView)? Long I have a UIScrollView which pages horizontally. A ‘page’…
alloy
  • 20,908
  • 2
  • 30
  • 40
5
votes
3 answers

how to delete the diagonal elements of a matrix in MATLAB?

I need a code to omit the diagonal elements of a matrix for example if A = [1 2 3; 1 2 3; 1 2 3]; the the output come: [2 3; 1 3; 1 2]; how can i do it simply ( i know a long one but i need it simple)
Mohamad Pishdad
  • 107
  • 1
  • 5
4
votes
3 answers

Fastest way to expand the values of a numpy matrix in diagonal blocks

I'm searching for a fast way for resize the matrix in a special way, without using for-loops: I have a squared Matrix: matrix = [[ 1, 2, 3, 4, 5], [ 6, 7, 8, 9,10], [11,12,13,14,15], [16,17,18,19,20], …
4
votes
1 answer

Passing multiple arguments as one in Julia

I am trying to make a diagonal block matrix in Julia. I have an nxn array that I want to make P copies of as a block matrix down the diagonal and the rest of the matrix is sparse. If arr is my array, I know I can…
Nick k
  • 43
  • 3
4
votes
3 answers

How to concatenate matrices in diagonal form in Julia

I have a matrix A and I want to build a block diagonal matrix M as A 0 ⋯ 0 0 A ⋯ 0 ⋮ ⋮ ⋱ ⋮ 0 0 ⋯ A where the 0s are matrices of the same size as A filled with zeros. Is there a convenient way to do that? In other words, is there an equivalent to…
Muratani
  • 49
  • 4
4
votes
1 answer

Solving Generalized eigenvectors and eigen values to get a common basis

I am looking for solving a generalized eigenvectors and eigen value problem in Matlab. For this, I have tested 2 methods. if Generalized problem is formulated as : Then, we could multiply by B^(-1) on each side, such as : So, from a theorical…
user1773603
4
votes
4 answers

Julia - How to efficiently turn to zero the diagonal of a matrix?

In Julia, what would be an efficient way of turning the diagonal of a matrix to zero?
Davi Barreira
  • 1,597
  • 11
  • 19