How to Convert Matlab for-loop in Python
21 次查看(过去 30 天)
显示 更早的评论
I have a code in Matlab and need to convert it to python:
for idx = 1: numel(alpha)
x(idx) = radius * math.cos(math.pi/180*alpha(idx))
x(idx) = x (idx) + center(1)
y(idx) = radius * math.sin(math.pi/180*alpha(idx))
y(idx) = y (idx) + center(2)
end
I converted the first line with numpy as np to:
for idx in range(1, np.nummel(alpha)):
but i dont know how to convert the other lines.
It is an compute for an circle.
Thanks for every help.
0 个评论
采纳的回答
Yongjian Feng
2021-11-20
Your code looks like mixture of matlab and python already. math.sin is python, not matlab. Try this:
import math
# Not sure you are usign 0-based or 1-based. You need to check that. python is normally 0-based
for idx in range(0, np.nummel(alpha)):
x[idx] = radius * math.cos(math.pi/180*alpha[idx])
x[idx] = x[idx] + center[1]
y[idx] = radius * math.sin(math.pi/180*alpha[idx])
y[idx] = y[idx] + center[2]
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!