简体   繁体   中英

How do I efficiently apply a transform to a multi-channel numpy ndarray?

I have three channels of data. The channels represent x, y and z coordinates. For example, the x position for a point is stored in channel 1, the y position in channel 2 etc.

I would like to perform a transform on each point represented in these channels. That is, I would like to multiply x, y and z at each u, v position by a transformation matrix. Sure, I could manually iterate through each u, v position and extract the positional data. I feel like that is not the most efficient approach.

My question is, what is the most efficient approach to transforming a collection of points, where positional data is stored in channels corresponding to each dimension?

What you describe is simply a matrix multiplication:

raw = numpy.array([ [1, 10, 100], [2, 20, 200], [3, 30, 300], [4, 40, 400] ])
transformation = numpy.matrix([ [1, 0, 0], [0, 0, 1], [0, -1, 0] ])
transformed = raw * transformation

Perhaps your data are higher-dimensional than this M-datapoints-by-3-"channels" example? It's unclear what you mean by u and v but they sort-of-suggest your problem might be higher-dimensional. If so, you could either investigate numpy.tensordot and numpy.einsum which allow generalization of matrix multiplication to higher dimensions, or you could set raw to a view of your actual raw data that you then reshape down to M-by-3 (and undo that at the end by reshaping transformed back the way you want it).

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