# How to Convert Matlab for-loop in Python

조회 수: 45 (최근 30일)
Nils Conradf . 2021년 11월 14일
답변: Yongjian Feng . 2021년 11월 20일
I have a code in Matlab and need to convert it to python:
for idx = 1: numel(alpha)
x(idx) = x (idx) + center(1)
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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

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] = x[idx] + center
y[idx] = y[idx] + center

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!