Converting Python to Matlab
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Here is my Python code...
import numpy as np
import matplotlib.pyplot as plt
import copy
def SourceDistOnPt(xc,yc,xp,yp,theta):
ups=np.zeros((len(xc),len(xp)-1))
wps=np.zeros((len(xc),len(xp)-1))
for i in range(len(xc)):
for j in range(len(xp)-1):
r1=((xc[i]-xp[j])**2+(yc[i]-yp[j])**2)**(1/2)
r2=((xc[i]-xp[j+1])**2+(yc[i]-yp[j+1])**2)**(1/2)
nu2=np.arctan2(-(xc[i]-xp[j])*np.sin(theta[j])+(yc[i]-yp[j])*np.cos(theta[j])+(xp[j+1]-xp[j])*np.sin(theta[j])-(yp[j+1]-yp[j])*np.cos(theta[j]),\
(xc[i]-xp[j])*np.cos(theta[j])+(yc[i]-yp[j])*np.sin(theta[j])-(xp[j+1]-xp[j])*np.cos(theta[j])-(yp[j+1]-yp[j])*np.sin(theta[j]))
nu1=np.arctan2(-(xc[i]-xp[j])*np.sin(theta[j])+(yc[i]-yp[j])*np.cos(theta[j]),(xc[i]-xp[j])*np.cos(theta[j])+(yc[i]-yp[j])*np.sin(theta[j]))
ups[i,j]=1/(4*np.pi)*np.log(r1**2/r2**2)
wps[i,j]=1/(2*np.pi)*(nu2-nu1)
return ups,wps
I am trying to test the output of my Matlab code. I watched this video https://www.mathworks.com/videos/managing-code-in-matlab-functions-of-multiple-inputs-and-outputs-97211.html
I am trying to obtain a set of outputs for every input. I am not sure if I would add the for loops after the end for the function.
Matlab code...
function [r1, r2, nu2, nu1, ups, wps] = SourceDistOnPt(xc, yc, xp, yp, theta)
ups = zeros(length(xc), length(xp)-1);
wps = zeros(length(xc), length(xp)-1);
for i = 1:length(xp) - 1
for j = 1:length(xc) - 1
r1 = ((xc(i) - xp(j)).^2 + (yc(i) - yp(j)).^2)^(1/2.0);
r2 = ((xc(i) - xp(j+1)).^2 + (yc(i) - yp(j+1)).^2).^(1/2.0);
nu2 = atan2(-(xc(i)-xp(j)) * sin(theta(j)) + (yc(i) - yp(j)) * cos(theta(j))+(xp(j+1) - xp(j)) * sin(theta(j))-(yp(j+1) - yp(j)) * cos(theta(j)),...
(xc(i) - xp(j)) * cos(theta(j)) + (yc(i) - yp(j)) * sin(theta(j)) - (xp(j+1) - xp(j)) * cos(theta(j)) - (yp(j+1) - yp(j)) * sin(theta(j)));
nu1 = atan2(-(xc(i)-xp(j)) * sin(theta(j)) + (yc(i) - yp(j)) * cos(theta(j)), (xc(i) - xp(j)) * cos(theta(j)) + (yc(i) - yp(j)) * sin(theta(j)));
ups(i, j) = 1 /(4 * pi) * log(r1.^2/r2.^2);
wps(i, j) = 1 /(2 * pi) * (nu2-nu1);
end
end
r1 = ((xc(i) - xp(j)).^2 + (yc(i) - yp(j)).^2)^(1/2.0);
r2 = ((xc(i) - xp(j+1)).^2 + (yc(i) - yp(j+1)).^2).^(1/2.0);
nu2 = atan2(-(xc(i)-xp(j)) * sin(theta(j)) + (yc(i) - yp(j)) * cos(theta(j))+(xp(j+1) - xp(j)) * sin(theta(j))-(yp(j+1) - yp(j)) * cos(theta(j)),...
(xc(i) - xp(j)) * cos(theta(j)) + (yc(i) - yp(j)) * sin(theta(j)) - (xp(j+1) - xp(j)) * cos(theta(j)) - (yp(j+1) - yp(j)) * sin(theta(j)));
nu1 = atan2(-(xc(i)-xp(j)) * sin(theta(j)) + (yc(i) - yp(j)) * cos(theta(j)), (xc(i) - xp(j)) * cos(theta(j)) + (yc(i) - yp(j)) * sin(theta(j)));
ups(i, j) = 1 /(4 * pi) * log(r1.^2/r2.^2);
wps(i, j) = 1 /(2 * pi) * (nu2-nu1);
Respuestas (2)
Haritha
el 5 de Jun. de 2018
Editada: Walter Roberson
el 5 de Jun. de 2018
I have the code in python
n=6;
for a, b in product(range(n), range(n))
i need this code in matlab. if anyone knows please let me know
1 comentario
Walter Roberson
el 31 de Oct. de 2017
for i = 1:length(xp) - 1
Should not have the -1
Python range(n) gives 0 to n-1 but to use as a MATLAB index you need to add 1, so 1:n
Notice that the Python code subtracted 1 from the upper bound but that you coded the same as the one that did not subtract 1. The MATLAB translation of the second one does need to subtract one on the upper bound.
Perhaps it would help to define
PyIdx = @(K) K+1
PyRange = @(N) 0:N-1
And then copy the Python code using the appropriate wrapper, like
for i = PyRange(len(xc))
for j = PyRange(len(xc) - 1)
r1=((xc(PyIdx(i)) -xp(PyIdx(j))**2+(yc(PyIdx(i)) -yp(PyIdx(j))**2)**(1/2);
Get the code working first with something that is an obvious equivalent of the Python. You can always optimize later.
17 comentarios
Walter Roberson
el 9 de Nov. de 2017
He wants data to be saved on every run?
The pathName line you showed from Python does not save data. It might be setting up a file name for a later command to save data.
The savepath() command from MATLAB does not save data, it saves the current MATLAB path -- the list of directories to search for code. See https://www.mathworks.com/help/matlab/data-import-and-export.html for more on saving data.
Ver también
Categorías
Más información sobre MATLAB Compiler en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!