Reputation: 35
I'm trying to create a matrix of shape Nx3 where N is not known at first. This is what I'm basically trying to do:
F = np.array([[],[],[]])
for contact in contacts:
xp,yp,theta = contact
# Create vectors for points and normal
P = [xp, yp, 0]
N = [np.cos(theta), np.sin(theta), 0]
# Calculate vector product
cross_PN = np.cross(P,N)
# f = [mz, fx, fi]
mz = cross_PN[2]
fx = N[0]
fy = N[1]
f = np.array([mz, fx, fy])
F = np.vstack([F, f])
But this code doesn't work. I can do similar thing in Matlab very easily, but that is not the case in Python using numpy.
Any help is greatly appreciated. Thank you
I would like to create a matrix by adding new rows, but in the beginning the matrix is empty. That is why I receive the error: "along dimension 1, the array at index 0 has size 0 and the array at index 1 has size 3"
Upvotes: 1
Views: 171
Reputation: 35
Based on your comments and suggestion, I guess the correct and more efficient way to do this is the following:
L = len(contacts)
F = np.zeros((L, 3))
for ii in range(L):
xp,yp,theta = contacts[ii]
# Create vectors for points and normal
P = [xp, yp, 0]
N = [np.cos(theta), np.sin(theta), 0]
# Calculate vector product
cross_PN = np.cross(P,N)
# f = [mz, fx, fi]
mz = cross_PN[2]
fx = N[0]
fy = N[1]
fi = np.array([mz, fx, fy])
F[ii] = fi
Upvotes: 1