Help converting Python to Matlab

2 次查看(过去 30 天)
Zach Dunagan
Zach Dunagan 2017-12-1
For now I will start with this single line of code.
Python
dPSto1[:,n]=1/2*rho*(np.reshape((Uinf**2-tangVelFoil**2),(numPanels,))-2*np.reshape(si,(numPanels,))*(tangVelSto[:,t]-tangVelSto[:,t-1])/tStep)
Matlab
dPSto1(:, n) = 1 / 2 .* rho * (reshape((Uinf.^2 - tangVelFoil.^2),[numPanels,1]) - 2 .* reshape(si, [numPanels, 1]) .* (tangVelSto(:, t) - tangVelSto(:, t-1)) / tStep);
How do I display the code in the code text box? Does it automatically appear after you copy and paste the code?
What I have been doing with the code is braking down the Python into smaller bits. For example:
aa = 1/2*rho*(np.reshape((Uinf**2-tangVelFoil**2),(numPanels,))
bb = etc.
I would do the same for Matlab and compare the individual parts of the code. For some reason the Python code isn't running when I brake it up into smaller sections. It was working the other day, but not now. Currently working on it.
EDIT: I think I figured it out. What do you all think about these numbers? They are close, but not exact... Images attached are the output for both Python and Matlab. My guess is Matlab and Python calculate numbers differently...
  5 个评论
Guillaume
Guillaume 2017-12-3
To display code properly, you either put two spaces before each line, or select all the code then press the {}Code button above the edit box.
Answers is a public forum where people asks questions that are relevant not only for themselves but potentially to others. In return, others answer the question so that it can help everybody.
If you ask a question, get an answer and then edit the question to replace it by a completely different question that makes the whole exercise pointless. When people now look at the question they see an answer that is completely unrelated.
The proper way this forum works is: you ask a question (possibly clarify the question if you get questions in return), get answers, accept an answer if it answer your question, then start another question in a different post.
Zach Dunagan
Zach Dunagan 2017-12-3
Okay, sounds good. I will definitely keep this in mind when asking questions. Thank you.

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2017-12-1
I don't know Python but in most languages the % operator is either integer division or the modulo operator. A quick search reveals that it is the modulo operator in python. Thus, in matlab:
if mod(t, 1) == 0
which simply tests that t is integer.
I've now idea how np.reshape works, but clearly in matlab [numPanels,] is pointless (if it's not a syntax error) as it is the same as [numPanels]. Assuming that the left out argument in python just means work it out from the rest, the matlab equivalent is
reshape(something, numPanels, [])
You also have to watch out that matlab indexing is 1 based whereas Python uses 0-based indexing. Your n and t may need adding +1 when used as indices.
  1 个评论
Zach Dunagan
Zach Dunagan 2017-12-3
编辑:Zach Dunagan 2017-12-3
I got it all figured out. Can you show me a example of how to print out multiple images/frames and save them to a folder? How do you integrate it into the for loop?
How would I use filepath = 'C://Users//Zach Dunagan//Desktop//Frame//' save all the images to this folder.

请先登录,再进行评论。

类别

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!

Translated by