Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arrays in MATLAB and PYTHON
#1
Hello everyone, I am a newbie at Python. I want to "translate" my code in MATLAB into Python but meet a lot of problems.
For example, this is my code in MATLAB: To create a circle
alpha = 0:2*pi/10000:2*pi;
R = 100000;
for i = 1:length(alpha)
x(i) = R*cos(alpha(i))
y(i) = R*sin(alpha(i))
end
plot(x,y,'r')
Can anyone else help me do it in Python please? Thank you very much.
Reply
#2
Show what have you tried and what "problems" you meet.
Post your code in python tags, any traceback - in full, in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
import math
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd

def fx(R, alpha):
    x = R*math.cos(alpha)
    y = R*math.sin(alpha)
    return x,y

alpha = np.linspace(0, math.pi*2, num = 10000)
Rmax = 100000
for i in range(alpha):
    (x[i], y[i]) = fx(Rmax, alpha[i])
Error:
TypeError Traceback (most recent call last) <ipython-input-14-8444e88ffbda> in <module> 2 alpha = np.linspace(0, math.pi*2, num = 10000) 3 Rmax = 100000 ----> 4 for i in range(alpha): 5 (x[i], y[i]) = fx(Rmax, alpha[i]) 6 TypeError: only integer scalar arrays can be converted to a scalar index
Here is my code and error.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Matlab to Python Sateesh 0 1,636 Nov-04-2020, 09:11 AM
Last Post: Sateesh
  From Matlab to Python erbab 1 1,968 Oct-27-2020, 02:16 PM
Last Post: jefsummers
  Matlab to Python -- Parallel Computing zistambo 1 1,934 Jun-10-2020, 04:59 PM
Last Post: pyzyx3qwerty
  Python v MatLab for graphs and plots CynthiaMoore 4 2,979 Apr-22-2020, 02:13 PM
Last Post: CynthiaMoore
  python equivalent to MATLAB xcov chai0404 2 3,817 Apr-02-2020, 10:29 PM
Last Post: chai0404
  Python equivalent of Matlab code kwokmaster 1 3,399 Mar-25-2020, 10:14 PM
Last Post: j.crater
  No arrays in Python? twilkinson 2 1,759 Jan-29-2020, 04:42 PM
Last Post: ThiefOfTime
  MATLAB to Python conversion stokd 10 4,917 Jan-19-2020, 09:14 PM
Last Post: stokd
  Help converting MATLAB triple-for loop to Python davlovsky 1 1,952 Oct-29-2019, 10:26 PM
Last Post: scidam
  Python sorts all arrays instead of one. Alex009988 2 1,992 Sep-02-2019, 02:31 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020