2017-02-09 96 views
0

通过my_matrix [np.tril_indices(68)]将矩阵68x68转换为2346个元素的矢量。用它做了一些事情,之后我想重新将它重构为68x68的矩阵。如何从矢量Python制作矩阵

最终矩阵应该如下。我的矩阵只会在对角线的下半部分。休息将被归零。正如你可以从我的原始矩阵看到图像:

enter image description here

注:绿色是矢量值,红色是零。

回答

2

使用来自np.tril_indices相同指数重建 -

N = 68 # Number of rows/cols in original array 
r,c = np.tril_indices(N) # Get the lower triangular region indices 

# Initialize o/p array and assign from data_array (2346) elems array 
out = np.zeros((N,N),dtype=data_array.dtype) 
out[r,c] = data_array