I have a 2d matrix with dimension (3, n) called A
, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
let say A is:
A=[[-0.00022939 -0.04265404 0.00022939]
[ 0. -0.2096513 0. ]
[ 0.00026388 0.00465183 0.00026388]]
def vectors(b):
b = b/np.sqrt(np.sum(b**2.,axis=0))
b = b/np.linalg.norm(b)
z = np.array([0.,0.,1.])
n1 = np.cross(z,b,axis=0)
n1 = n1/np.linalg.norm(n1) ## normalize n
return [n1]
n1 = vectors(A)
How can I make a loop that picks the first column and makes the calculation, then the second column and so on. Any help!!. Thank in advance
It depends on how you set up your array to start with. I like to use numpy arrays as I find the indexing easier to get my head around. I think the below code is what you are after. As you always have 3 colulmns it doesnt matter how long A is, you can just slice it into 3 columns.
import numpy as np
A=np.array([[-0.00022939, -0.04265404, 0.00022939],
[-0.00022939, -0.04265404, 0.00022939],
[0., -0.2096513, 0.],
[0.00026388, 0.00465183, 0.00026388]])
for idx in range(3):
b = A[:, idx]
print b # call your function here
EDIT:: Full implementation showing the code & the output
import numpy as np
def vectors(b):
b = b/np.sqrt(np.sum(b**2.,axis=0))
b = b/np.linalg.norm(b)
z = np.array([0.,0.,1.])
n1 = np.cross(z,b,axis=0)
n1 = n1/np.linalg.norm(n1) ## normalize n
return [n1]
A=np.array([[-0.00022939, -0.04265404, 0.00022939],
[ 0., -0.2096513, 0. ],
[ 0.00026388, 0.00026388, 0.00026388]])
for idx in range(3):
b = A[:, idx]
n1 = vectors(b)
print 'idx', idx, '\nb ', b, '\nn1 ', n1, '\n'
Output:
idx 0
b [-0.00022939 0. 0.00026388]
n1 [array([ 0., -1., 0.])]
idx 1
b [-0.04265404 -0.2096513 0.00026388]
n1 [array([ 0.9799247 , -0.19936794, 0. ])]
idx 2
b [ 0.00022939 0. 0.00026388]
n1 [array([ 0., 1., 0.])]
You can try this:
A=[[1,2,3],[4,5,6],[7,8,9]]
def getColumn(m):
res=[]
for x in A:
res.append(x[m])
return res
def countSomething(x):
# counting code here
print x
def looper(n): # n is the second dimension size
for x in xrange(0,n):
countSomething(getColumn(x))
looper(3)
Controller Advice bean not instantiated at proper order in Spring Boot 2.4
What should I enter to the connection string to connect my NodeJS application to a MongoDB server?
Unable to use Computed Property Values with Dots - Unable to Set as String - JS
I'm looking for a powerful and fast way to handle processing of large file in Google App Engine
I am trying to evaluate the density of multivariate t distribution of a 13-d vectorUsing the dmvt function from the mvtnorm package in R, the result I get is
Given a pandas dataframe in the following format:
I found come old TCP reverse shell made in python online and im editing it to fix it up, when it prints the output of the command it doesnt print it all if the command is too bigThis is the code for sending the commands and then printing the output