# get the column from 2d array to calculate the normalization and cross product in python

556
December 15, 2016, at 12:44 PM

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]]``````
how can I take the first column( -0.00022939, 0., 0.00026388) from A and use it in the function below, then take then second column, ... n column

``````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

Answer 1

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.])]
``````
Answer 2

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)
``````
POPULAR ONLINE

#### XML MySql Error [on hold]

READ ALSO ### How to handle processing of large file on GAE?

I'm looking for a powerful and fast way to handle processing of large file in Google App Engine

343 ### Density of multivariate t distribution in Python for large number of observations

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

344 ### Pandas Dataframe grouping and standard deviation

Given a pandas dataframe in the following format:

505 ### Python - Printing out all data reveived from a socket

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

293