简体   繁体   中英

Avoiding for loop to iterate over an array in Python

I am a beginner in Python, trying to implement computer vision algorithms.I have to iterate over image read as a 2 dimensional array several times and I want to avoid using for loops.

For example, I want to multiply camera matrix P(3x4 dimension) with each row of coordinate matrix, where each row is dimension 1x4. I will of course take transpose of the row vector for matrix multiplication. Here is how I have implemented it using for loop. I initialize an empty array. Cameras is an object instance. So I loop over the object to find the total number of cameras. Counter gives me the total number of cameras. Then I read through each row of matrix v_h and perform the multiplication. I would like to accomplish the below task without using for loop in python. I believe it's possible but I don't know how to do it. For the number of points in thousands, using for loop is becoming very inefficient. I know my code is very inefficient and would appreciate any help.

   for c in cameras:
     counter=counter+1

   for c in cameras:       
     v_to_s=np.zeros((v_h.shape[0],c.P.shape[0],counter),dtype=float)
     for i in range(0,v_h.shape[0]):
       v_to_s[i,:,cam_count]=np.dot(c.P,v_h[i,:].T)

numpy具有可以执行乘法的matmul()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM