Converting Python to Matlab

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 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);

回答(2 个)

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
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 个评论

Would this need to be placed in a function file or non function file?
Based on what I have read here it looks like it's a non function file. https://www.mathworks.com/examples/matlab/mw/matlab-ex27866669-functions-with-multiple-inputs-or-outputs
These functions can be created in a .m file.
The inputs are xc, xp, yc, yp. The for loops depend on i and j, but i and j depend on xc and xp.
= @(K) K+1 is an example of an anonymous function. It is an executable statement that can be used at any time any other executable statement can be used. It creates a variable in the local workspace, which would disappear when the workspace was destroyed. You could also create trivial .m files if you wanted to have them accessible from everywhere:
function K1 = PyIdx(K)
K1 = K+1;
end
and
function r = PyRange(N)
r = 0 : N-1;
end
The python lines
def SourceDistOnPt(xc,yc,xp,yp,theta):
[...]
return ups,wps
correspond to MATLAB
function [ups, wps] = SourceDistOnPt(xc,yc,xp,yp,theta)
and the code should be saved in SourceDistOnPt.m
I meant function file, not .m file. A function file is an .m file, but I was referring to the name.
You should be creating a .m file that starts with 'function' for this purpose.
I called it SourceDistOnPt.
function [ups, wps] = SourceDistOnPt(xc, yc, xp, yp, theta)
ups = zeros(length(xc), length(xp));
wps = zeros(length(xc), length(xp));
for i = 1:length(xc)
for j = 1:length(xp)
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
Trying to find the error that reads index exceed matrix dimension.
As I remarked on earlier, the Python
for j in range(len(xp)-1):
translates to
for j = 1:length(xp)-1
I changed it. Now I am to the point of using the first function file, where the function name is SourceDistOnPt. There error I get is "Incorrect number of arguments". The primary script file and function file is attached. Currently, looking for the error.
You have
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))
Without even counting brackets to check where the atan2() call ends, we can quickly see that there is no comma in that line, and so the line has too few input arguments to the atan2() call.
I think I have all the function files working properly now with the primary script.
I'd like to discuss this bit of code with you. I just want to make sure I am interpreting it correctly.
A = zeros(numPanels + 1, numPanels + 1);
A(1:end - 1, 1:end - 1) = nSource;
A(1:end - 1, end) = sum(nVortex, 2);
A(end, 1:end - 1) = tSource(1, :) + tSource(end, :);
Line 1: I am making 'A' matrix zeros which is a 65 x 65 in my case.
Line 2: Take 'A' subtracts one row at the end and subtracts one column at the end. Then replaces zeros with nSource?
Line 3: Subtracts one row at the end and subtracts all columns after 1st column and then sums each column returns in a 2D matrix.
Line 4: Take last row and all columns except one at the end of 'A', replaces it with tSource first row and all columns adds to tSource last row and all columns?
I would rephrase some of that.
Line 2: stores nSource into the sub-array of A without the last row or column
Line 3: sums nVortex along the rows, creating a column vector, and storing that column vector as the last column of A except for the last row
Line 4: adds the first and last row of tSource, creating a row vector, and stores that as the last row of A except for the last column.
This all leaves A(end,end) as 0 from the initial assignment to A.
You need to read all actions on the right hand side of the "=" as being done first and independently of what is happening on the left hand side.
Python...
pathName='C://Users//Zach Dunagan//Desktop//frames//'
Matlab
status = savepath('C:\FramesMatlab')
Are these equivalent statements? I have a folder named 'FramesMatlab' this is where save data would go when running the script? I am not quite understanding what is going on here.
The python code appears to only change a variable. The MATLAB code appears to save a copy of the current MATLAB path to C:\FramesMatlab\pathdef.m . Different operations.
The grad student I work with told me files needs to be saved every time the code runs. Do you know what he means by this?
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.
I will ask him tomorrow. I think I am misunderstanding what he wants exactly.

请先登录,再进行评论。

类别

帮助中心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!

Translated by