Matlab to Python - extracting lower subdiagonal triangle, why different order?

Pave

I am translating code from MATLAB to Python. I need to extract the lower subdiagonal values of a matrix. My attempt in python seems to extract the same values (sum is equal), but in different order. This is a problem as I need to apply corrcoef after.

The original Matlab code is using an array of indices to subset a matrix.

MATLAB code:

values = 1:100;
matrix = reshape(values,[10,10]);

subdiag = find(tril(ones(10),-1));

matrix_subdiag = matrix(subdiag);

subdiag_sum = sum(matrix_subdiag);
disp(matrix_subdiag(1:10))
disp(subdiag_sum)

Output:
2 3 4 5 6 7 8 9 10 13

1530

My attempt in Python

import numpy as np

matrix = np.arange(1,101).reshape(10,10)
matrix_t = matrix.T #to match MATLAB arrangement

matrix_subdiag = matrix_t[np.tril_indices((10), k = -1)]

subdiag_sum = np.sum(matrix_subdiag)
print(matrix_subdiag[0:10], subdiag_sum))

Output: [2 3 13 4 14 24 5 15 25 35] 1530

How do I get the same order output? Where is my error?

Thank you!

mozway

For the sum use directly numpy.triu on the non-transposed matrix:

S = np.triu(matrix, k=1).sum()
# 1530

For the indices, numpy.triu_indices_from and slicing as a flattened array:

idx = matrix[np.triu_indices_from(matrix, k=1)]

output:

array([ 2,  3,  4,  5,  6,  7,  8,  9, 10, 13, 14, 15, 16, 17, 18, 19, 20,
       24, 25, 26, 27, 28, 29, 30, 35, 36, 37, 38, 39, 40, 46, 47, 48, 49,
       50, 57, 58, 59, 60, 68, 69, 70, 79, 80, 90])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why are some Python exceptions lower-case?

Copy upper triangle to lower triangle in a python matrix

Couldn't understand why Java and Scala lower bounds behave different

Plot lower triangle in a seaborn Pairgrid

Extracting values of upper triangle by row order

Why is the triangle a different colour shade to the menu background?

Extract lower triangle portion of a matrix

Faster approach extracting lower triangle of multidimensional array using Numpy?

flattening of a numpy array along columns, in the order: lower triangle, diagonal, upper triangle

breeze Copy lower triangle not working?

matrix - mirror lower triangle to upper triangle

Python generate a mask for the lower triangle of a matrix

How can I create a correlation matrix from 2 vectors of its upper and lower triangle in python?

Extracting different Data from a txt file in python

Storing lower triangle of a matrix into an array

How to copy lower triangle to upper triangle for a 4 dimension array in Numpy Python?

Lower Triangle Matrix

Create a block diagonal and subdiagonal with a repeating block in Matlab

why keys() of dictionary in python returns in a different order?

python - extracting values from dictionary in order

Why do i get different results when i use lower() and when i not use lower()?

Lower triangle mask with seaborn clustermap

fill lower triangle (including diagonal) of a Numpy Array with -infinity - Python

Extracting lower triangle and project it into another 2D space

why Plot Python 3 different with Matlab and even excel

Why do these sort in different order?

why does islower have to be after .lower() in python

Why does Opencv Imgproc.minAreaRect give different results for the same points (that make up an asymmetrical triangle) in a different order?

Python- Why are my outputs different than my inputs for a right triangle?