w3resource logo

:

Python Exercises

Python: Convert cartesian coordinates to polar coordinates

Python NumPy: Random Exercise-14 with Solution

Write a Python program to convert cartesian coordinates to polar coordinates of a random 10x3 matrix representing cartesian coordinates.

Sample Solution:-

Python Code:

import numpy as np
z= np.random.random((10,3))
x,y = z[:,0], z[:,1]
r = np.sqrt(x**2+y**2)
t = np.arctan2(y,x)
print(r)
print(t)

Sample Output:

[ 0.46197382  0.07034582  0.89015703  1.06227375  1.29780158  0.32823362                                                                      
  0.99823096  0.74005334  0.38023205  0.62537423]                      
[ 1.05405564  1.09799508  0.75642993  0.97273388  0.86502465  0.88995172                                                                      
  0.3019899   1.05539822  0.91697477  1.22828465]

Python Code Editor:

Improve this sample solution and post your code through Disqus