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

How to find sum of elements above secondary diagonal in matrix?

I tried searching for the solution to this problem for a while now and couldn't find anything. How do I sum the elements above the secondary diagonal in a matrix (just with loops, nothing fancy) in Java? This is what I tried: public static void…
Roy
  • 1,612
  • 2
  • 12
  • 38
-1
votes
1 answer

How to build a diagonal matrix in python?

I have matrix a which has the shape of [100,100], and matrix b of the same shape [100,100]. They are filled with some values. What I want to do, is to build such diagonal matrixes [[a1,0],[0,b1]] for each element of a and b. What is the best to do…
John
  • 3
  • 2
-1
votes
1 answer

How to fix the segmentation error in the code?

Sample Input 3 11 2 4 4 5 6 10 8 -12 Sample Output 15 Explanation The primary diagonal is: 11 5 -12 Sum across the primary diagonal: 11 + 5 - 12 = 4 The secondary diagonal is: 4 5 10 Sum across the secondary diagonal: 4 + 5…
-1
votes
1 answer

defining diagonals in python

How would I go about creating the matrix [[ 1 2 0 0 0] [-1 1 2 0 0] [ 0 -1 1 2 0] [ 0 0 -1 1 2] [ 0 0 0 -1 1]] using the numpy.diag() function in Python? I want to define the main diagonal and also the parallel diagonals of…
-1
votes
1 answer

set up a diagonal matrix faster?

Calling diag(x) is apparently very slow. Is there not a faster way to set up a diagonal matrix? It seems like a fairly easy operation, yet R takes forever. Also, using the diagonal matrix later on in multiplications is also extremely slow. So if I…
Janus
  • 165
  • 4
-1
votes
1 answer

Extract the diagonal element of the matrix

I need to extract the elements of the main diagonal of the matrix: I have tried the following solution: [U S V]= svd (T) lambda= reshape(S',[],1); But I have got the column vector with all elements but I need only the elements of the main…
-1
votes
3 answers

How do I get the left diagonal elements of a matrix/grid from a specific element?

I have an 8x8 grid of different numbers, and I want to get the elements of the diagonal that contains a given starting position. Here is an example l = [[str(randint(1,9)) for i in range(8)] for n in range(8)] >> [ [1 5 2 8 6 9 6 8] [2 2 2 2 8 2 2…
Altaaf Ackbar
  • 99
  • 1
  • 9
-1
votes
2 answers

Initialise a diagonal matrix in python

i'd like create in python a matrix with all zeros except for some value selected from a list. For example (very stupid) from l=[0,1,2,3] i'd like create a matrix (a list of list) with the letter "x" in position l[0] l[1] etc.. like this: [x, 0,…
farso92
  • 41
  • 5
-1
votes
2 answers

When M is an odd number and if it is middle of diagonal it shows Zero after diagonal interchange

If the value of M is 3, and if value at mat[1][1] is 5, then after interchanging mat[1][1] with mat[1][1] it should be still 5, but instead it is showing 0. But when M is an even number, the code works perfect. #include #define M 3 void…
Zamir Manihar
  • 121
  • 12
-1
votes
1 answer

Changing the diagonal elements of BlockMatrix

I have a block matrix of size 4*4 Blockmatrix = 0.0 2.0 1.0 2.0 2.0 0.0 2.0 4.0 1.0 2.0 0.0 3.0 2.0 4.0 3.0 0.0 While typing the type of this matrix, It is showing
Mani Rz
  • 87
  • 4
  • 11
-1
votes
2 answers

Traverse a 2D array converted to 1D, diagonal

I want to traverse a 2D square array that I have converted to 1D. The problem is that I want to traverse it as if I was traversing the original 2D in diagonal strips. The array is diagonal and I originally created it with malloc in 1D to avoid…
guruste
  • 5
  • 1
  • 1
  • 4
-1
votes
1 answer

to print all the diagonal elements for a given array

a = [['a','b','c'], ['d','e','f','g'], ['h','i','j','k'], ['l','m','n']] I need to print the diagonal elements of the given array such as output would be: [['l'],['h','m'],['d','i','n'],['a','e','j'],['b','f','k'],['c','g']]
Pankaj
  • 65
  • 2
  • 9
-1
votes
1 answer

Compute how many possibilities of summing numbers from a matrix that are equal with another number

I have this problem where I need to compute the number of ways in which elements that are consecutive in a matrix can be summed in order to obtain a certain number. Let me give you an example : Searched number = 42 Matrix : 00 03 07 09 10 09 …
-1
votes
3 answers

Diagonal difference code not working

import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ int sum1=0,…
-1
votes
1 answer

exchange the elements in diagonal in Array - C#

I'd like to know how to write a program in 2D array to exchange the elements of main diagonal with secondary diagonal then print the result like this 3 2 7 4 5 3 2 8 9 and the result show like this 7 2 3 4 5 3 9 8 2
user6234753
  • 71
  • 1
  • 1
  • 5