1

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 n x n matrix.

Thank you

1
  • 1
    Please format properly and ask again. Commented Jul 2, 2020 at 23:21

2 Answers 2

3
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)
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent answer. Exactly what I was looking for. Thank you very much @hpaulj
2

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)]

2 Comments

What a great answer. I find this really useful. Thanks so much S_Zizzle.
Works great and it is very fast ! Thank you very much @S_Zizzle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.