0

I am translating some MATLAB code and am having some problems understanding the syntaxical difference between MATLAB and numpy for Python.

In MATLAB I have a 6 by 6 matrix, A, as well as 6 float values, a, b, c, d, e, f. The MATLAB code

B = A*[a;b;c;d;e;f];

produces a 6 by 1 matrix, B. So I figured that the MATLAB operator '*' must correspond to the numpy operator numpy.dot().

So in my Python code I have the same matrix, A, and the same values for a, b, c, d, e and f.

B = numpy.dot(A, [[a],[b],[c],[d],[e],[f]])

does not produce the same matrix, B, and neither does

B = numpy.dot(A, [a,b,c,d,e,f])

I have also tried building the Python matrix and array with numpy's array function with the same result. It feels like I am mixing something fundamental up here. Any help is greatly appreciated.

8
  • In Matlab [a;b;c;d;e;f] is a column vector, are you sure your numpy vectors are column vectors and not row vectors? Commented Jul 22, 2015 at 12:54
  • Do you have a numpy matrix or an array? wiki.scipy.org/… Commented Jul 22, 2015 at 12:54
  • 1
    Also, you will get a much better response if you explain what the difference is between what you expected and what you got instead of making people guess! Commented Jul 22, 2015 at 12:54
  • 1
    In Matlab, if A is 6 by 6, A*[a;b;c;d;e;f] should be 6 by 1 (i.e. a column vector). Are you sure you got a 1 x 6 matrix? Commented Jul 22, 2015 at 12:58
  • 4
    Please include code in your question that we can run (i.e. include the code that defines A, a, b, etc), and show the output that you get in Matlab and python. Commented Jul 22, 2015 at 13:02

2 Answers 2

3

It's difficult to answer fully without knowing the values in A, and a,b,c,d,e,f. However, you're code seems to work:

MATLAB:

A = [1,2,3,4,5,6;
     1,2,3,4,5,6;
     1,2,3,4,5,6;
     1,2,3,4,5,6;
     1,2,3,4,5,6;
     1,2,3,4,5,6];

b = [7;8;9;10;11;12];

A*b

ans = 

  217
  217
  217
  217
  217
  217

A'*b

ans = 

   57
  114
  171
  228
  285
  342

Python:

import numpy as np

A = np.array([[1,2,3,4,5,6],
              [1,2,3,4,5,6],
              [1,2,3,4,5,6],
              [1,2,3,4,5,6],
              [1,2,3,4,5,6],
              [1,2,3,4,5,6]])

b = np.array([7,8,9,10,11,12]).reshape(6, 1)

B = np.dot(A, b)

B
array([[217],
       [217],
       [217],
       [217],
       [217],
       [217]])

B = np.dot(A.transpose(), b)

B
array([[57],
       [114],
       [171],
       [228],
       [285],
       [342]])

The numpy dot operator does perform matrix multiplication, so it is likely that something is going wrong with your initialisation of A which you don't show.

Note that the reshape operation is not necessary (the same results are seen regardless). However, this makes a column vector rather than a 1D array and so can be transposed etc. in the same fashion as the MATLAB array [7;8;9;10;11;12].

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

1 Comment

@WarrenWeckesser True, I've changed it to a reshape. It's not necessary but it makes the point that this is a column vector which matches the equivalent MATLAB code that was posted (and follows the matrix multiplication rules). I'll also add a note explaining this.
0

The vector you are multiplying is a column, so use it as a column to have a final 6x1 vector and not a 1x6 vector, as you are doing a dot product of 6x6 by 6x1 in MATLAB.

This example is for a 3x3 dimension (just to reduce the code and be understandable):

import numpy as np
A = np.array([[8,1,6],[3,5,7],[4,9,2]])  # This is the matrix
b = np.array([1,2,3])                    # These are the float numbers
c = np.dot(A,b.reshape(-1,1))            # This is a 3x1 vector

This is equivalent on MATLAB to:

magic(3)*[1;2;3]

Comments

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.