Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have 2 numpy array with equal shape.

V = [[-1 -1 -1] [-2 -2 -2] [-3 -3 -3]]
U = [[1 2 3] [2 3 4] [3 4 5]]

I want to convert matlab to python for below line.

Ot = U*([V(:,1) V(:,2) -V(:,3)])';

I want to convert this matlab code in python.
How can I do that? Inside V(:,1) and V(:,2) are multiplying?

share|improve this question
1  
What is the expected output here? –  Ashwini Chaudhary Jan 17 at 9:41
    
You yourself have asked multiplying arrays previously: stackoverflow.com/questions/21179499/… –  zhangxaochen Jan 17 at 9:48

1 Answer 1

up vote 1 down vote accepted

It's not multiplying V(:,1) by V(:,2), it's just separating them by white space. code [V(:,1) V(:,2) -V(:,3)] in matlab just generates matrix:

>> [V(:,1) V(:,2) -V(:,3)]

ans =

    -1    -1     1
    -2    -2     2
    -3    -3     3

so the equivalent could be:

In [90]: V = np.array([[-1, -1, -1], [-2, -2, -2], [-3, -3, -3]])
    ...: U = np.array([[1, 2, 3], [2, 3, 4], [3, 4, 5]])

In [91]: V[:, 2]*=-1

In [92]: Ot = U.dot(V.T)

In [93]: Ot
Out[93]: 
array([[ 0,  0,  0],
       [-1, -2, -3],
       [-2, -4, -6]])
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.