'Matlab To Python conversion: SyntaxError: cannot assign to function call
This is my MATLAB code it runs fine.
% all of the given time are in milisecond
n = 3; % No of image
T = 100; % for the time period user wants to see the mirage
ts = .2*(100/(2*n-3)); % standing time
tv = .6*((100-((2*n-3)*ts))/(2*(n-1))); % travelling time
m1 = 0:.1:tv;
x1 = .5*(1-cos(pi*(m1/tv))); %position equation
xa = x1;
%travelling toward from left to right
for i= 1:n-1
x1 = x1+i-1;
%standing at one point
for f = 1:ts
x2(f) = x1(end);
end
if i==1
x3=[x1,x2];
else
x3 = [x3,x1,x2];
end
end
I'm trying to turn it into Python code. It shows SyntaxError: cannot assign to function call
This is Python code I came up with
import numpy as np
import math
n = 3;
T = 100;
ts = .2*(100/(2*n-3));
tv = .6*((100-((2*n-3)*ts))/(2*(n-1)));
m1 = np.arange(0,tv,0.1);
x1 = 0.5*(1-(np.cos(np.pi*(m1/tv))));
xa = x1;
##################################
#travelling toward from left to right
for i in range(1,n-1):
x1 = x1+i-1;
for f in np.arange(1,ts, ):
x2(f) = x1(end); # The error line
if i==1:
x3 = np.array([x1,x2]);
else:
x3 = np.array([x3,x1,x2]);
How can I fix this? what changes can I make in the x2(f) = x1(end) because whatever I do it shows different error every time.
Solution 1:[1]
First of all I would recommend stop using semicolons. One of main python existence reasons is to make the code more readable.
Then, I would recommend not using blank arguments, it has no practical use if you do not do this intentionally:
for f in np.arange(1,ts):
About your program, you are doing something like function(some specific argument) assign function(argument under 'end' variable), if you want to operate with arrays you should use square brackets, curly are reserved for functions:
x2[f] = x1[end]
Solution 2:[2]
Indexing and slicing in python uses [] not ()
for i in range(1, n-1):
x1 = x1 + i - 1;
x2 = []
for f in np.arange(1, ts):
x2.append(x1[-1])
if i == 1:
x3 = np.array([x1, x2])
else:
x3 = np.array([x3, x1, x2])
Solution 3:[3]
Regarding the
TypeError: list indices must be integers or slices, not numpy.float64
Look at what the happens in Octave:
Initially x2 is undefined, but is created when we start the f loop:
>> x2
error: 'x2' undefined near line 1 column 1
>> f=1
f = 1
>> x2(f) = x1(end)
x2 = 1
If I try to index it with the last value, ts, I get the same sort of error:
>> f=ts
f = 6.6667
>> x2(f)
error: x2(6.66667): subscripts must be either integers 1 to (2^63)-1 or logicals
You don't get the error in the actual loop, because the 1:ts iteration actually creates integers in the range:
>> 1:ts
ans =
1 2 3 4 5 6
In numpy:
In [1]: ts = 6.6667
In [3]: np.arange(0,ts)
Out[3]: array([0., 1., 2., 3., 4., 5., 6.])
While arange steps by 1s, the actual values are float, hence the indexing error.
And a variable, array, cannot be created by this assignment:
In [6]: x2[0]=1
Traceback (most recent call last):
Input In [6] in <cell line: 1>
x2[0]=1
NameError: name 'x2' is not defined
Correct ways of creating such an array/list in Python:
In [14]: x2 = np.zeros(6)
...: for i in range(6):
...: x2[i] = i**2
...:
In [15]: x2
Out[15]: array([ 0., 1., 4., 9., 16., 25.])
In [16]: alist = []
...: for i in range(6):
...: alist.append(i**2)
...:
...:
In [17]: alist
Out[17]: [0, 1, 4, 9, 16, 25]
But your f loop just puts the same value in all slots
for f = 1:ts
x2(f) = x1(end);
end
for that we really should use whole-array methods:
In [21]: x2 = np.full(6,fill_value=2.3)
In [22]: x2
Out[22]: array([2.3, 2.3, 2.3, 2.3, 2.3, 2.3])
or:
In [25]: x2=np.zeros(6)
...: x2[:] = 2.3
In MATLAB you can get away with lots of iterations, since it does jit compilations. But numpy is like the original MATLAB, where compiled whole-array methods are much faster.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KubaJ |
| Solution 2 | |
| Solution 3 | hpaulj |
