我正在将代码从MatLab翻译成Python.我需要求出矩阵的下次对角线值.我在python语言中的try 似乎提取了相同的值(sum等于),但顺序不同.这是一个问题,因为我需要在之后使用Corrcoef.
最初的MatLab代码使用一组索引来子集一个矩阵.
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))
输出: [2 3 13 4 14 24 5 15 25 35]1530
如何获得相同的订单输出?我的错误在哪里?
谢谢!