输出没有来。绘图问题 [Matlab 到 python 的转换]
Output doesn't come. Plotting issue [Matlab to python conversion]
这是我的 Pyhton 代码。
import numpy as np
import matplotlib.pyplot as plt
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 = np.arange(0,tv,0.1);
x1 = 0.5*(1-(np.cos(np.pi*(m1/tv))));
xa = x1;
x2 = []
#travelling toward from left to right
for i in np.arange(1,n-1):
x1 = x1+i -1
#standing at one point
for f in np.arange(1,ts):
x2.append(x1[-1])
if i==1:
x3 = [x1, x2]
else:
x3 = [x3,x1,x2]
#%travelling from right to left
xd = np.flip(xa)
for i in range(0, n - 1):
if i == 0:
pass
else:
xd = xd - 1
xw = []
for f in np.arange(1, ts):
mini = np.amin(xd)
xw.append(mini)
if i == 0:
xm = np.concatenate((xd.reshape(1, -1), np.array(xw).reshape(1, -1)), axis=1)
else:
xm = np.concatenate((xm, xd.reshape(1, -1), np.array(xw).reshape(1, -1)), axis=1)
xm = abs(np.amin(xm)) + xm
#%cloning the cycle
xs = []
for r in np.arange(1, np.fix(T/(2*(n-1)*(tv+ts)))):
if r==1:
xs.append[x3,xm]
else:
xs.append[xs,x3,xm]
plt.plot(xs)
但是当我 运行 它什么也没有出来时
这是我的 matlab 代码
clc;
close all;
clear all;
% 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
disp(x2)
%travelling from right to left
xd = flip(xa);
for i= 1:n-1
if i==1
else
xd = xd-1;
end
%standing at one point
for f = 1:ts
xw(f) = min(xd);
end
if i==1
xm=[xd,xw];
else
xm = [xm,xd,xw];
end
end
xm = abs(min(xm))+xm;
#%cloning the cycle
xs = []
for r in np.arange(1, np.fix(T/(2*(n-1)*(tv+ts)))):
if r==1:
xs.append(x3,xm)
else:
xs.append(xs,x3,xm)
plt.plot(xs)
这是matlab代码自带的输出。
我希望我的 python 代码的输出与 Matlab 代码相同,或者至少我想看到一个图。但它没有显示任何内容。那么我该如何解决这个问题呢?提前谢谢你。
这是您的代码的固定版本。基本思想是不要将 concatenate
与 append
混用。在 MATLAB 中,[[a],[b]]
是水平连接,不同于 Python 中的 list.append()
。还要注意 MATLAB 中的包含范围与 Python 中的独占范围,以及 fix
returns 和 float
而 int
returns 和 integer
.
import numpy as np
import matplotlib.pyplot as plt
# 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 = 0.2*(100/(2*n-3)) # standing time
tv = 0.6*((100-((2*n-3)*ts))/(2*(n-1))) # travelling time
m1 = np.arange(0,tv+.1,0.1)
x1 = 0.5*(1-np.cos(np.pi*(m1/tv))) # position equation
xa = x1
# travelling toward from left to right
x2 = np.zeros(int(ts))
for i in range(1,n):
x1 = x1+i-1
# standing at one point
for f in np.arange(0,int(ts)):
x2[f] = x1[-1]
if i == 1:
x3 = np.concatenate((x1,x2))
else:
x3 = np.concatenate((x3,x1,x2))
# # travelling from right to left
xd = xa[::-1] # flip xa
xw = np.zeros(int(ts))
for i in range(1,n):
if i != 1:
xd -= 1
# standing at one point
for f in np.arange(0,int(ts)):
xw[f] = np.min(xd)
if i == 1:
xm = np.concatenate((xd,xw))
else:
xm = np.concatenate((xm,xd,xw))
xm += np.abs(np.min(xm))
# cloning the cycle
for r in range(0,int(T/(2*(n-1)*(tv+ts)))):
if r == 0:
xs = np.concatenate( (x3,xm) )
else:
xs = np.concatenate( (xs,x3,xm) )
plt.plot(xs)
显示与 MATLAB 相同的绘图。
这是我的 Pyhton 代码。
import numpy as np
import matplotlib.pyplot as plt
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 = np.arange(0,tv,0.1);
x1 = 0.5*(1-(np.cos(np.pi*(m1/tv))));
xa = x1;
x2 = []
#travelling toward from left to right
for i in np.arange(1,n-1):
x1 = x1+i -1
#standing at one point
for f in np.arange(1,ts):
x2.append(x1[-1])
if i==1:
x3 = [x1, x2]
else:
x3 = [x3,x1,x2]
#%travelling from right to left
xd = np.flip(xa)
for i in range(0, n - 1):
if i == 0:
pass
else:
xd = xd - 1
xw = []
for f in np.arange(1, ts):
mini = np.amin(xd)
xw.append(mini)
if i == 0:
xm = np.concatenate((xd.reshape(1, -1), np.array(xw).reshape(1, -1)), axis=1)
else:
xm = np.concatenate((xm, xd.reshape(1, -1), np.array(xw).reshape(1, -1)), axis=1)
xm = abs(np.amin(xm)) + xm
#%cloning the cycle
xs = []
for r in np.arange(1, np.fix(T/(2*(n-1)*(tv+ts)))):
if r==1:
xs.append[x3,xm]
else:
xs.append[xs,x3,xm]
plt.plot(xs)
但是当我 运行 它什么也没有出来时
这是我的 matlab 代码
clc;
close all;
clear all;
% 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
disp(x2)
%travelling from right to left
xd = flip(xa);
for i= 1:n-1
if i==1
else
xd = xd-1;
end
%standing at one point
for f = 1:ts
xw(f) = min(xd);
end
if i==1
xm=[xd,xw];
else
xm = [xm,xd,xw];
end
end
xm = abs(min(xm))+xm;
#%cloning the cycle
xs = []
for r in np.arange(1, np.fix(T/(2*(n-1)*(tv+ts)))):
if r==1:
xs.append(x3,xm)
else:
xs.append(xs,x3,xm)
plt.plot(xs)
这是matlab代码自带的输出。 我希望我的 python 代码的输出与 Matlab 代码相同,或者至少我想看到一个图。但它没有显示任何内容。那么我该如何解决这个问题呢?提前谢谢你。
这是您的代码的固定版本。基本思想是不要将 concatenate
与 append
混用。在 MATLAB 中,[[a],[b]]
是水平连接,不同于 Python 中的 list.append()
。还要注意 MATLAB 中的包含范围与 Python 中的独占范围,以及 fix
returns 和 float
而 int
returns 和 integer
.
import numpy as np
import matplotlib.pyplot as plt
# 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 = 0.2*(100/(2*n-3)) # standing time
tv = 0.6*((100-((2*n-3)*ts))/(2*(n-1))) # travelling time
m1 = np.arange(0,tv+.1,0.1)
x1 = 0.5*(1-np.cos(np.pi*(m1/tv))) # position equation
xa = x1
# travelling toward from left to right
x2 = np.zeros(int(ts))
for i in range(1,n):
x1 = x1+i-1
# standing at one point
for f in np.arange(0,int(ts)):
x2[f] = x1[-1]
if i == 1:
x3 = np.concatenate((x1,x2))
else:
x3 = np.concatenate((x3,x1,x2))
# # travelling from right to left
xd = xa[::-1] # flip xa
xw = np.zeros(int(ts))
for i in range(1,n):
if i != 1:
xd -= 1
# standing at one point
for f in np.arange(0,int(ts)):
xw[f] = np.min(xd)
if i == 1:
xm = np.concatenate((xd,xw))
else:
xm = np.concatenate((xm,xd,xw))
xm += np.abs(np.min(xm))
# cloning the cycle
for r in range(0,int(T/(2*(n-1)*(tv+ts)))):
if r == 0:
xs = np.concatenate( (x3,xm) )
else:
xs = np.concatenate( (xs,x3,xm) )
plt.plot(xs)
显示与 MATLAB 相同的绘图。