我试图在Python中复制一些Matlab代码,这是我的Matlab代码:
for k = 1:N
if x(k) > 0.6 %triggers, when Amplitude is above 0.6
timeOver = ['Abnormal reading detected at Time ', num2str(t(k)), 's. Please change cutting tool !!']; % creating a warning sign to display when abnormality is present
disp(timeOver) % displaying of warning sign
h = msgbox(timeOver,'Warning','warn');
break % 'break' command for stopping of 'for' loop
end
end
我做了以下替换:
for k = 1:N
已更改为for k in numpy.arange(0, N, 1)
if x(k) > 0.6
已更改为if x.k > 0.6
我意识到在Matlab中,disp(k)
会产生值,而在Python print(k)
中会产生0.0
但是,Matlab中的for循环会生成一条消息,而Python不会生成任何内容。
答案 0 :(得分:0)
这段代码应解决问题:
import numpy as np
x = np.random.rand(10) # some values of x; replace it with your values of x
t = np.arange(1, 11) - (np.random.rand(10) - 0.5) # simulated, not equally spaced timestamps; replace it with your values of t
for idx, val in enumerate(x):
if val > 0.6: # triggers, when Amplitude is above 0.6
print('Abnormal reading detected at Time ' + str(t[idx]) + 's. Please change cutting tool !!')
break # 'break' command for stopping of 'for' loop