0

I am trying to fill a 2D array using a for loop, with a variable that is an array. From this array, I am hoping to find the average of each column and input this into a new array.

However, I don't know how to input the variable into a 2D array.

A_values = numpy.zeros([20, 1])

print(A_values)

for i in range(0,20):
    A_values([1,i]) = xa

In this code, xa is a variable that changes with each iteration and has a length of 402.

I am trying to get it so that the 2d array 'A_values' has 402 columns and 20 rows

2 Answers 2

1

Are you necessarily looking to use for-loops? I am asking because this problem can be solved in simpler and more efficient ways such as:

import numpy as np

a_values = np.random.rand(20,402) #Store random values in a_values with a shape of 20 rows and 402 columns
avg_columns = a_values.mean(axis=0) #Calculate the mean of each column 

print(avg_columns)

Documentation for generating random values: numpy.random.rand

EDIT:

I assumed that xa is randomized in each iteration (make sure to replace it with the xa that you are generating). You can initialize an empty a_values array, and add the generated xa array to it in each iteration using np.vstack. For the first iteration, when a_value is empty, I made it equal to xa (if a_values.size else xa).

import numpy as np

a_values = np.array([])

for i in range(20):
  xa = np.random.rand(1,402)
  a_values = np.vstack([a_values, xa]) if a_values.size else xa

avg_columns = a_values.mean(axis=0) #Average of each column

Documentation for vstack: numpy.vstack

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, I should have been clearers the array isn't filled with random numbers it is filled with integers representing the number of A particles left in a radioactive decay process. Thank you though!
No problem. I edited my post, is this what you wanted?
Yes, thank you again! the problem in my code turned out to be using brackets around square brackets continuing to give me a call function error. Thank you!
1

I don't think you can change the shape of A_values after it's defined. So you should start with:

A_values = numpy.zeros([20,402])

And if your for loop is iterating through the rows, your index should be [i, 1] (numpy array indexes are not like cartesian plane coords).

But you want to change entire rows of A_values, to do this you use just the first index:

A_values[row] = some_row

Remember that "some_row" needs to have the proper lenght, otherwise you get a ValueError.

Your final code will look like this:

A_values = numpy.zeros([20, 402])

for i in range(0,20):
    A_values([i]) = xa

PS: You should describe the errors you are getting and be clearer if you want help with the "xa" values as well...

4 Comments

thank you for your help! However, I am getting an error which reads "can't assign to function call" . xa = numpy.concatenate([evolve_system(NA, NB, NC, rules1, nsteps)[0], evolve_system(final_NA, final_NB, final_NC, rules2, nsteps)[0]]), where evolve_system is a function previously defined.
What does evolve_system returns? To use numpy.concatenate(), both arguments must be numpy.arrays and you need to use the right axis. Have a look here: docs.scipy.org/doc/numpy/reference/generated/… (i actually think the error is in the evolve_system function now)
evolve_systems returns an array of integers.
you should remove this outer pair of square brackets: numpy.concatenate(evolve_system(NA, NB, NC, rules1, nsteps)[0], evolve_system(final_NA, final_NB, final_NC, rules2, nsteps)[0])

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.