1

I am trying create an numpy array that contain sub arrays. The output that i am looking for should be like this:

[[0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
 21.  22.5 24.  25.5 27.  28.5 30.] [0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5 21]......]

but instead i am getting just a single array as below

[0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
 21.  22.5 24.  25.5 27.  28.5 30. 0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5 21........]

The background of this is that i have an array of arrays called "b".that array looks like this: [array([421.3, 448.6, 449.32171103, 444.28498751, 449.36065693, 448.75383007, 449.25048692, 448.75383007, 448.59001326, 448.64239657, 448.64239657, 448.00558032, 448.00558032, 447.93972809, 448.44620636, 447.93972809, 447.93972809, 447.87609894, 447.64383163, 447.6985593 , 447.21918563]), array([447.75551365, 447.75551365, 448.36146132, 447.75551365, 447.75551365, 447.75551365, 447.75551365, 448.36146132, 447.6985593 , 448.36146132, 447.6985593 , 447.59133146, 447.6985593 , 447.54105957, 447.64383163, 447.54105957, 446.87805943, 446.87805943, 446.75720475, 446.70012313, 446.70012313, 446.70012313, 446.64527312, 446.64527312, 446.14907822, 445.88002871, 445.70169396, 445.29989894]).........] i need to plot each array and i want to create another similar array of arrays with matching lengths but with different content using the code below. here is my code can you please suggest how to fix this.

tt=np.array([])

for i in range(len(array_size)):
    time_calc_1=0
    for j in range(len(b[i])):
    
        tt=np.append(tt,time_calc_1)
        time_calc_1=time_calc_1+1.5
1
  • np.append docs say that without an axis parameter, the inputs are raveled (made 1d) and joined on that one axis - hence the result is always 1d. Do not treat np.append as a list append clone. Commented Sep 17, 2020 at 15:17

1 Answer 1

1

Do not loop and append to array. It is a bad idea. Instead use functions to achieve your goal:

tt = np.repeat(np.arange(0,31,1.5)[None,:],25,0)

output:

[[ 0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
  21.  22.5 24.  25.5 27.  28.5 30. ]
 [ 0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
  21.  22.5 24.  25.5 27.  28.5 30. ]
 [ 0.   1.5  3.   4.5  6.   7.5  9.  10.5 12.  13.5 15.  16.5 18.  19.5
  21.  22.5 24.  25.5 27.  28.5 30. ]
...

UPDATE: In case of variable length arrays, (I suggest to use list of lists, but if array of arrays is insisted):

list of lists:

b = [10,20,30]
tt = [np.arange(0,i,1.5).tolist() for i in b]
#[[0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0], [0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5, 15.0, 16.5, 18.0, 19.5], [0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5, 15.0, 16.5, 18.0, 19.5, 21.0, 22.5, 24.0, 25.5, 27.0, 28.5]]

array of arrays:

b = [10,20,30]
tt = np.array([np.arange(0,i,1.5) for i in b])
#[array([0. , 1.5, 3. , 4.5, 6. , 7.5, 9. ])
# array([ 0. ,  1.5,  3. ,  4.5,  6. ,  7.5,  9. , 10.5, 12. , 13.5, 15. ,  16.5, 18. , 19.5])
# array([ 0. ,  1.5,  3. ,  4.5,  6. ,  7.5,  9. , 10.5, 12. , 13.5, 15. ,  16.5, 18. , 19.5, 21. , 22.5, 24. , 25.5, 27. , 28.5])]
Sign up to request clarification or add additional context in comments.

11 Comments

Sorry I think i was not clear with my code. In my case the size of the arrays can vary. its not fixed as i have in the code in the question. both loops have the range that may vary depending on the size of the variable.tt=np.array([]) for o in range(array_size): time_calc_1=0 for oo in range(b[o])): tt=np.append(tt,time_calc_1) time_calc_1=time_calc_1+1.5
@Malik You still can do a lot with functions. Can you provide a more realistic example to your case so we can help better? Also note that you cannot have a non-rectangular array.
@Malik based on your update in your comment, you cannot have a non-rectangular array (e.g.[[1,2],[3]] does not exist in numpy). I would suggest to build a list of lists. However, another not recommended way to go is an array of arrays/lists (which is quite useless in terms of using numpy). If you rewrite your question to a correct desired output, I can update the answer here. Thank you.
In my case the array b[] contain sub arrays of varying length. what i am trying to do is to generate another array with sub arrays of same length as b[] but with different content. the array_size is this case is 25 but could be different
But what is the point of using array of arrays? nonetheless, I will update the answer to include such example.
|

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.