0

I have a (68x2) matrix named shape and I am trying to iterate through all the 68 rows by placing column 0 and column 1 of shape in array B. This is then multiplied by a (3x3) transformation matrix A. Then my intent was to create a single array (which is why I used np.append) but actually all I am getting are 68 singular 2 dimensional matrices and I do not know why.

Here is my code:

import numpy as np

for row in shape:
    B = np.array([[row[0]],[row[1]],[1]])
    result = np.matmul(A,B)
    result = np.append(result[0], result[1], axis = 0)
    print(result) 

Anyone know how I can fix my problem?

2
  • 1
    If you need to iterate through rows, collect the results in a list, and turn that into an array once at the end. np.append is EVIL. Commented Mar 10, 2021 at 21:02
  • I created an empty list result = [] and at the end I added result.append(result) is that what you meant because I'm getting: AttributeError: 'numpy.ndarray' object has no attribute 'append' Commented Mar 10, 2021 at 21:10

1 Answer 1

2

You can concatenate a new column onto your shape array and then multiply all your rows by the transform matrix at once using a single matrix multiplication.

result = (np.concatenate((shape, np.ones((68, 1))), axis=1) @ A)[:,:2]

It's possible you need to multiply by the transpose of the transformation matrix, A.T, rather than by A itself.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this worked wonders. For my case I did not need to use the transpose of A

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.