Home > Mobile >  Matlab To Python conversion: SyntaxError: cannot assign to function call
Matlab To Python conversion: SyntaxError: cannot assign to function call

Time:04-26

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.

CodePudding user response:

Indexing and slicing in python uses [] not ()

for i in range(1,n-1):
    x1 = x1 i-1;
    for f in np.arange(1,ts, ):     
        x2[f] = x1[-1];            # The error line
        if i==1:
            x3 = np.array([x1,x2]);
        else:
            x3 = np.array([x3,x1,x2]); 

CodePudding user response:

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]   
  • Related