Matlab function to Python

Viewed 61

I know these are the questions I'm asking from months now, but I still have problems:

Matlab:

gy=tanh(alpha.*xup.'*w)

This is how I translated it to Python:

GY = np.tanh(np.multiply(np.multiply(Alpha, np.transpose(XUp)), WSeg)

It looks correct, except for the fact that GY is different from Matlab to Python. In Matlab - with sz = size(gy) - it says:

sz =

 1     2

In Python - with print(GY.shape) - it says:

(2, 128)

what's wrong ? I can assure you that the variables used match perfectly since this point.

1 Answers

This MATLAB code

n = 3;
m = 4;
k = 5;
alpha = (1 + 2i) * ones(n, m);
xup  = (3 + 4i) * ones(m, n);
w = (5 + 6i) * ones(m, k);
alpha.*xup.'*w
ans =

  -340 +  80i  -340 +  80i  -340 +  80i  -340 +  80i  -340 +  80i
  -340 +  80i  -340 +  80i  -340 +  80i  -340 +  80i  -340 +  80i
  -340 +  80i  -340 +  80i  -340 +  80i  -340 +  80i  -340 +  80i```

does the same as this python code

import numpy as np
n = 3
m = 4
k = 5
alpha = np.full((n, m), 1 + 2j)
xup  = np.full((m, n), 3 + 4j)
w = np.full((m, k),  5 + 6j) 
print(alpha * xup.T @ w)
[[-340.+80.j -340.+80.j -340.+80.j -340.+80.j -340.+80.j]
 [-340.+80.j -340.+80.j -340.+80.j -340.+80.j -340.+80.j]
 [-340.+80.j -340.+80.j -340.+80.j -340.+80.j -340.+80.j]]

Note that alpha.*xup.' and alpha * xup.T are elementwise multiplications.

Related