Reputation: 61
I have a program in numpy utf8, which allows me to calculate the coordinates of a parabolic shot from the ground. I need to create a function which returns the coordinates (#1), create the different arrays of values to work with (#2), and finally use the function to generate the different coordinates for each pack of values
#1
def coordenadas(v,a,t,g=9.81):
rad=deg2rad(a)
x=v*cos(a)*t
y=v*sin(a)*t-(1./2.)*g*(t**2)
xy=array([x,y]).T
return xy
#2
v=arange(50,100,10) #m/s
adegree=arange(45,90,5) #degrees
a=deg2rad(adegree) #rads
t=linspace(0,10,50) #segundos
#3
v.shape=(5,1,1)
a.shape=(1,9,1)
t.shape=(1,1,50)
#5
XY=coordenadas(v,a,t,g=9.81)
print shape(XY)
print XY
#4
My question is that shape(XY) returns
(50L, 9L, 5L, 2L)
And XY (only a bit, is too long)
[[[[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]
[ 0. 0. ]]
And more boxes of this shape
What this really means(big boxes, boxes, small boxes, rows, columns) ???
Upvotes: 0
Views: 2973
Reputation: 3872
Numpy arrays are basically matrices, where each box [] represents the start of a new dimension. As an easy example the matrix
11
23
could be written in numpy as:
a = numpy.array([[1,1],[2,3]])
which then would be printed as
array([[1, 1],
[2, 3]])
As this is a two-dimensional matrix, the outer "box" marks the edges of the matrix, whereas the inner boxes are the rows of the matrix with the ,
separating the entries. Calling a.shape
without an argument gives the shape of the 2x2 matrix:
(2, 2)
Calling the shape
method with argument reshapes the matrix given to the shape defined in the argument. But to further help you with the code:
Your function definition seems to be totally fine, except I don't see a reason, why you export x and y in an array, rather than just returning two different values.
The initialization of your arrays seem to be fine as well.
There is totally no reason to reshape the arrays you just created, just leave them as they are.
You have to call the function separately with each set of values to create the coordinates. Do that by using an itteration over the arrays you just created.
Upvotes: 0
Reputation: 3608
(50L, 9L, 5L, 2L)
means a 4D
array.
You can visualize as a 50x9
matrix and each cell of this matrix contains a 5x2
matrix
Upvotes: 2