Home > OS >  How to split a two-dimensional array into different pieces and combine them into a new array
How to split a two-dimensional array into different pieces and combine them into a new array

Time:08-31

here is the code

v_in = 2.5
v_out = 25
v_step = 0.5
row = len(np.arange(v_in, v_out v_step, v_step))
Cp = [0.489, 0.4, 0.452, 0.45]
Cp_set = np.zeros((row, len(Cp) 1))
Cp_set[:,0] = np.arange(v_in, v_out v_step, v_step)

for i in range(len(Cp)):
    v_rate = (7150*1000/(0.5*1.225*195**2*Cp[i]/4))**(1/3)
    v_rate_index = np.ceil(v_rate/v_step)*v_step
    x = [Cp_set[int(v_rate_index)-10:int(v_rate_index)-5, 0], Cp_set[int(v_rate_index):row, 0]]
    print(x)

the result is below

[array([4.5, 5. , 5.5, 6. , 6.5]), array([ 9.5, 10. , 10.5, 11. , 11.5, 12. , 12.5, 13. , 13.5, 14. , 14.5,  15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5, 20. ,  20.5, 21. , 21.5, 22. , 22.5, 23. , 23.5, 24. , 24.5, 25. ])]

how did I get what I want as below

[4.5, 5. , 5.5, 6. , 6.5,  9.5, 10. , 10.5, 11. , 11.5, 12. , 12.5, 13. , 13.5, 14. , 14.5,  15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5, 20. ,  20.5, 21. , 21.5, 22. , 22.5, 23. , 23.5, 24. , 24.5, 25.]

CodePudding user response:

Use concatenation:

import numpy as np

v_in = 2.5
v_out = 25
v_step = 0.5
row = len(np.arange(v_in, v_out v_step, v_step))
Cp = [0.489, 0.4, 0.452, 0.45]
Cp_set = np.zeros((row, len(Cp) 1))
Cp_set[:,0] = np.arange(v_in, v_out v_step, v_step)

for i in range(len(Cp)):
    v_rate = (7150*1000/(0.5*1.225*195**2*Cp[i]/4))**(1/3)
    v_rate_index = np.ceil(v_rate/v_step)*v_step
    x = np.concatenate([[0],Cp_set[int(v_rate_index)-10:int(v_rate_index)-5, 0], Cp_set[int(v_rate_index):row, 0]])
    print(x)

Output:

[ 0.   4.5  5.   5.5  6.   6.5  9.5 10.  10.5 11.  11.5 12.  12.5 13.
 13.5 14.  14.5 15.  15.5 16.  16.5 17.  17.5 18.  18.5 19.  19.5 20.
 20.5 21.  21.5 22.  22.5 23.  23.5 24.  24.5 25. ]
[ 0.   5.   5.5  6.   6.5  7.  10.  10.5 11.  11.5 12.  12.5 13.  13.5
 14.  14.5 15.  15.5 16.  16.5 17.  17.5 18.  18.5 19.  19.5 20.  20.5
 21.  21.5 22.  22.5 23.  23.5 24.  24.5 25. ]
[ 0.   4.5  5.   5.5  6.   6.5  9.5 10.  10.5 11.  11.5 12.  12.5 13.
 13.5 14.  14.5 15.  15.5 16.  16.5 17.  17.5 18.  18.5 19.  19.5 20.
 20.5 21.  21.5 22.  22.5 23.  23.5 24.  24.5 25. ]
[ 0.   4.5  5.   5.5  6.   6.5  9.5 10.  10.5 11.  11.5 12.  12.5 13.
 13.5 14.  14.5 15.  15.5 16.  16.5 17.  17.5 18.  18.5 19.  19.5 20.
 20.5 21.  21.5 22.  22.5 23.  23.5 24.  24.5 25. ]

CodePudding user response:

you need to flatten your list like so :

for i in range(len(Cp)):
    v_rate = (7150*1000/(0.5*1.225*195**2*Cp[i]/4))**(1/3)
    v_rate_index = np.ceil(v_rate/v_step)*v_step
    x = [Cp_set[int(v_rate_index)-10:int(v_rate_index)-5, 0], Cp_set[int(v_rate_index):row, 0]]
    flatten_list = [element for sublist in x for element in sublist]
    print(flatten_list)

Output : [4.5, 5.0, 5.5, 6.0, 6.5, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5, 20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0, 23.5, 24.0, 24.5, 25.0]

  • Related