Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

python - Applying a list of linear transformations to a list of point clouds

Suppose I have an array of n point clouds (each containing m points, of dimension d), and then a list of n matrices (each one dxd), representing linear transformations. Is there a vectorized way to multiply each matrix with its corresponding point cloud? (I.e., multiply the first matrix with each point in the first point cloud, the second matrix with each point in the second point cloud, etc.)

For example, here's an example computation I'd like to be able to do, using a for loop:

import numpy as np
points = np.random.rand(3,5,2)
mats = np.array([np.eye(2), -np.eye(2), 2*np.eye(2)])
out = np.zeros((3,5,2))
for i in range(points.shape[0]):
    out[i,:] = np.matmul(points[i,:], mats[i])

In this case, every point in the first cloud should be unchanged, every point in the second cloud should have its sign flipped, and every point in the third cloud should have each component doubled.

I appreciate any help you can provide -- I've been trying to get this working for a while with no success!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't think you need to loop:

out = np.matmul(points, mats)

Or for Python 3.5+:

out = points @ mats

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...