简体   繁体   中英

Create an array of matrices from 1D arrays in python

Could you please help to create an array of matrices when elements are taken to be 1D arrays in python.

For Ex: Here is what I am trying to do

import numpy as np

ele_1 = np.linspace(0,1,num= 50)
ele_2 = np.linspace(1,2,num= 50)
ele_3 = np.linspace(2,3,num= 50)
ele_4 = np.linspace(3,4,num= 50)

Mat_array = np.array([[ele_1,ele_2],[ele_2,ele_4]]) #This is giving me (2, 2, 50) array


Expected output:

array([matrix_1,matrix_2,.....]

Here matrix_i is array([[ele_1[i],ele_2[i]],[ele_3[i],ele_4[i]]]) . Mat_array must be (50, 2, 2,) array

I want to avoid loops and this method should also be applicable to applicable any nxn matrix.

Thank you

In [153]: ele_1 = np.linspace(0,1,num= 50) 
     ...: ele_2 = np.linspace(1,2,num= 50) 
     ...: ele_3 = np.linspace(2,3,num= 50) 
     ...: ele_4 = np.linspace(3,4,num= 50)                                              
In [154]: Mat_array = np.array([[ele_1,ele_2],[ele_3,ele_4]]) # correction?             
In [155]: Mat_array.shape                                                               
Out[155]: (2, 2, 50)

transpose can put the 50 first:

In [156]: Mat_array.transpose(2,0,1).shape                                              
Out[156]: (50, 2, 2)
In [157]: Mat_array = np.array([[ele_1,ele_2],[ele_3,ele_4]]).transpose(2,0,1)          
In [158]: timeit Mat_array = np.array([[ele_1,ele_2],[ele_3,ele_4]]).transpose(2,0,1)   
7.56 µs ± 51 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

An alternative with np.stack on a new last axis:

In [159]: res = np.stack([ele_1,ele_2,ele_3,ele_4],axis=1).reshape(-1,2,2)              
In [160]: np.allclose(res, Mat_array)                                                   
Out[160]: True
In [161]: timeit res = np.stack([ele_1,ele_2,ele_3,ele_4],axis=1).reshape(-1,2,2)       
16.9 µs ± 69.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

but it's slower.

S_Zizzle's answer is slower, especially when returning an array. It iterates n times:

In [162]: final_array = np.array([[[ele_1[i],ele_2[i]],[ele_3[i],ele_4[i]]] for i in ran
     ...: ge(n)])                                                                       
In [163]: np.allclose(final_array, Mat_array)                                           
Out[163]: True
In [164]: timeit final_array = np.array([[[ele_1[i],ele_2[i]],[ele_3[i],ele_4[i]]] for i
     ...:  in range(n)])                                                                
188 µs ± 373 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

I think this should do what you want, let me know if it needs changing, happy to help. This method uses list concatenation, unsure wether you want this or not, but does avoid a for loop block.

import numpy as np

n = 50

arr_1 = np.linspace(0,1,num=n)
arr_2 = np.linspace(1,2,num=n)
arr_3 = np.linspace(2,3,num=n)
arr_4 = np.linspace(3,4,num=n)

final_array = [[[arr_1[i],arr_2[i]],[arr_3[i],arr_4[i]]] for i in range(n)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM