## Finding all elements below the big diagonal of a matrix

I have a matrix like the following:

matrix = [ ['a', 'p', 'p', 'l', 'e'], ['a', 'g', 'o', 'd', 'o'], ['n', 'n', 'e', 'r', 't'], ['g', 'a', 'T', 'A', 'C'], ['m', 'i', 'c', 's', 'r'], ['P', 'o', 'P', 'o', 'P']]

And i'm trying to find all the elements below the big diagonal. But I want them in a certain way, example, the ideal output would be:

["ocAt", "PsC","or","P"]

(I want the letters in the "rising diagonal to the right" way, here the big diagonal being "PiTro")

I tried something like:

for f in range(len(matrix) - 1, -1, -1): for k in range(len(matrix)-1, 0, -1): my_list.append(matrix[k][k-f])

But I either get an index out of range or not the good elements. If anyone has a solution, I'll be glad to know!

This is one way you can achieve that result:

matrix = [['a', 'p', 'p', 'l', 'e'], ['a', 'g', 'o', 'd', 'o'], ['n', 'n', 'e', 'r', 't'], ['g', 'a', 'T', 'A', 'C'], ['m', 'i', 'c', 's', 'r'], ['P', 'o', 'P', 'o', 'P']] my_list = [] for f in range(1, len(matrix[0])): s = [] for k in range(len(matrix[0]) - f): s.append(matrix[len(matrix) - k - 1][f + k]) my_list.append(''.join(s)) print(my_list) # ['ocAt', 'PsC', 'or', 'P']

Or using comprehension:

my_list = [''.join(matrix[len(matrix) - i - 1][j + i] for i in range(len(matrix[0]) - j)) for j in range(1, len(matrix[0]))]

To produce every substring in each diagonal you can do:

my_list = [] for j in range(1, len(matrix[0])): for i1 in range(0, len(matrix[0]) - j): for i2 in range(i1 + 1, len(matrix[0]) - j + 1): s = [] for i in range(i1, i2): s.append(matrix[len(matrix) - i - 1][j + i]) my_list.append(''.join(s)) print(my_list) # ['o', 'oc', 'ocA', 'ocAt', 'c', 'cA', 'cAt', 'A', 'At', 't', 'P', 'Ps', 'PsC', 's', 'sC', 'C', 'o', 'or', 'r', 'P']

Or equivalently:

my_list = [''.join(matrix[len(matrix) - i - 1][j + i] for i in range(i1, i2)) for j in range(1, len(matrix[0])) for i1 in range(0, len(matrix[0]) - j) for i2 in range(i1 + 1, len(matrix[0]) - j + 1)]

--

One solution for substrings in upper diagonals:

my_list = [] for i in range(len(matrix)): for j1 in range(min(i + 1, len(matrix[0]))): for j2 in range(j1, min(i + 1, len(matrix[0]))): s = [] for j in range(j1, j2 + 1): s.append(matrix[i - j][j]) my_list.append(''.join(s)) print(my_list) # ['a', 'a', 'ap', 'p', 'n', 'ng', 'ngp', 'g', 'gp', 'p', 'g', # 'gn', 'gno', 'gnol', 'n', 'no', 'nol', 'o', 'ol', 'l', 'm', # 'ma', 'mae', 'maed', 'maede', 'a', 'ae', 'aed', 'aede', 'e', # 'ed', 'ede', 'd', 'de', 'e', 'P', 'Pi', 'PiT', 'PiTr', 'PiTro', # 'i', 'iT', 'iTr', 'iTro', 'T', 'Tr', 'Tro', 'r', 'ro', 'o']

**Efficiently compute sums of diagonals of a matrix,** The secondary diagonal is formed by the elements A03, A12, A21, A30. A simple C++ program to find sum of diagonals Get hold of all the important DSA concepts with the DSA Self Paced Course at a y) in a matrix � Find smallest and largest element from square matrix diagonals � Check if sums of i-th� In this C Program to find Sum of Diagonal Elements of a Matrix example, We declared single Two dimensional arrays Multiplication of size of 10 * 10. Below statements ask the User to enter the Matrix size (Number of rows and columns.

Other way to do it:

width = len(matrix[0]) height =len(matrix) result = [''] * (width-1) number_elements_to_take = width-1 for row in reversed(matrix): i = 0 for element in row[width - number_elements_to_take:]: result[i] += element i+=1 number_elements_to_take -= 1

**Program to print the Diagonals of a Matrix,** Given a 2D square matrix, print the Principal and Secondary diagonals. The primary diagonal is formed by the elements A00, A11, A22, A33. Below is the implementation of the above approach. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry� D = diag (v) returns a square diagonal matrix with the elements of vector v on the main diagonal. D = diag (v,k) places the elements of vector v on the k th diagonal. k=0 represents the main diagonal, k>0 is above the main diagonal, and k<0 is below the main diagonal.

Using `numpy`

:

matrix = numpy.array([['a', 'p', 'p', 'l', 'e'], ['a', 'g', 'o', 'd', 'o'], ['n', 'n', 'e', 'r', 't'], ['g', 'a', 'T', 'A', 'C'], ['m', 'i', 'c', 's', 'r'], ['P', 'o', 'P', 'o', 'P']]) result_list = [''.join(numpy.flipud(matrix[i:,i:]).diagonal()) for i in range(1, len(matrix)-1)]

**Create diagonal matrix or get diagonal elements of matrix,** This MATLAB function returns a square diagonal matrix with the elements of vector v on the main diagonal. collapse all in page main diagonal, k>0 is above the main diagonal, and k<0 is below the main diagonal. Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™. 1 is the first element of first row and 4 is the last element of last row. The straight path that joins them is known as principal diagonal of matrix A. The leading diagonal also contains the elements 2 and 3. Therefore, the straight path which holds the elements 1, 2, 3 and 4 is leading diagonal of matrix A

Another NumPy solution using `trace`

:

>>> list(map(np.flipud(np.array(matrix, object)).trace, range(1, len(matrix)-1))) ['ocAt', 'PsC', 'or', 'P']

**6.4,** I have yet to find a good English definition for what a determinant is. The line below shows the two ways to write a determinant. It is the product of the elements on the main diagonal minus the product of the elements off the main diagonal. The matrix of minors is the square matrix where each element is the minor for� abelian group augmented matrix basis basis for a vector space characteristic polynomial commutative ring determinant determinant of a matrix diagonalization diagonal matrix eigenvalue eigenvector elementary row operations exam finite group group group homomorphism group theory homomorphism ideal inverse matrix invertible matrix kernel linear

**Question about matrix and diagonals,** Sum of elements below secondary diagonal. So, I wanna firstly see how does it solve in math. I know the following: It has to equal� Find the product of sum of two diagonals of a square Matrix; Generalized Fibonacci Numbers; Minimize count of divisions by D to obtain at least K equal array elements; Java Program to Multiply two Matrices of any size; Find sum of all Boundary and Diagonal element of a Matrix; C program to implement Adjacency Matrix of a given Graph

**Types of Matrices,** A matrix is said to be square if the number of rows is equal to the number of columns. A square matrix is said to be triangular if all of its elements below the main diagonal are zero or all of its elements above the main diagonal are zero. Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image. Example:

**[PDF] Diagonal Matrices, Upper and Lower Triangular Matrices,** Definition: A diagonal matrix is a square matrix with zero entries except Inverses: A diagonal matrix D is invertible if and only if all the diagonal elements are An upper triangular matrix is a square matrix in which all entries below the main. numpy.diagonal (a, offset=0, axis1=0, axis2=1) [source] ¶ Return specified diagonals. If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of the form a[i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and axis2 are