1

I'm converting MATLAB code to Python

This is my code in python:

import numpy as np
import math

n=150
L=1
inter=L/n
y=np.linspace(inter/2,L-inter/2,n).transpose()
E=(210000000000)*np.ones(n)
Rho=7800*np.ones(n)
PI=math.pi
A=np.exp( 5+2*y*(np.sin(2*PI*y/L)) )*0.000001

This works fine up until this point with no difference in values or issues until I have to execute this piece of MATLAB code.

Mass=sum(Rho*inter.*A)

I tried the same using np.sum(Rho*inter*A) and just Rho*inter*A

The first case I got a single answer 1.0626206716847877 but MATLAB returns a 150 element array.

In the scond case I got an ndarray like I wanted but the values were not the same as what I got in MATLAB.

Values I got in MATLAB : matlab values pastebin

Values I got in python : python values pastebin

What am I doing wrong?

1
  • 1
    This bit of MATLAB code smells: Rho*inter.*A. Do you know the order of precedence? Is it (Rho*inter).*A or is it Rho*(inter.*A)? In the first case you have element-wise multiplication of two vectors, in the second case you have a matrix product. These are most likely very different results! I recommend being explicit about operator ordering in cases like these where it matters, by adding parenthesis. Commented Apr 23, 2020 at 13:48

3 Answers 3

1
(Rho[:,None]*inter*A).sum(axis=0) 

matches your MATLAB pastebin.

Or using einsum to sort out the axes:

np.einsum('i,j->j', Rho,inter*A)

which just reduces to:

Rho.sum() * inter*A

Is that really what you are trying to do in MATLAB?

It might help if you showed the actual MATLAB code used to create Rho, A etc.

Mass=sum(Rho*inter.*A)

What's the size of Rho and A in MATLAB? One may be [1x150], but the other? Is Rho [1x150] also, or [150x150]. The * is matrix multiplication, like @ in numpy, but .* is elementwise.

In the numpy code y, Rho and A all have shape (150,). The transpose on y does nothing. Rho*inter*A is elementwise multiplication producing a (150,) as well.

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

1 Comment

Thank you for this answer, np.einsum('i,j->j', Rho,inter*A) worked like a charm and matched the values i was looking for. But im still a little confused on how that worked, could you explain?. I'm sorry if this sounds a little noobish, im learning while translating code hence the doubts.
1

NumPy always sums all elements of a matrix. MATLAB's default is column-based, i.e. all of your 150 columns sum to a single total, hence the array. Use sum(matrix,'all'); in MATLAB to sum over all elements in a matrix. If you have a MATLAB older than 2018b, use sum(matrix(:)), i.e. store your matrix in a temporary variable, then flatten it to a column before summing.

To sum over columns in Python, specify the axis, being 0: np.sum(matrix,axis=0)

numpy.sum():

Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array.

sum() from MATLAB:

S = sum(A) returns the sum of the elements of A along the first array dimension whose size does not equal 1.
If A is a matrix, then sum(A) returns a row vector containing the sum of each column.
S = sum(A,'all') computes the sum of all elements of A. This syntax is valid for MATLAB® versions R2018b and later.

To prevent this kind of unclarities, I prefer to always specify which direction to sum over, i.e. sum(matrix,1) for MATLAB and np.sum(matrix,axis=0) for NumPy, regardless of the default.

Comments

-1

I think that in MATLAB using sum on a matrix you will get the sum of its individual columns and you will end up with an array with its number of elements equal to that of the columns. Use one more sum command in MATLAB: sum(sum(M)), which is the equivalent of np.sum(M) in Python.

2 Comments

sum(sum(M)) is an unnecessary complication, as well as being badly scalable (what if you have 15 dimensions?). See my answer for the short syntax.
Also, OP wants to sum over columns in Python, like they do in MATLAB. Not the other way around (summing over the entire matrix in MATLAB as they do in Python)

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.