Borrar filtros
Borrar filtros

Help converting Python to Matlab

3 visualizaciones (últimos 30 días)
Zach Dunagan
Zach Dunagan el 1 de Dic. de 2017
Comentada: Zach Dunagan el 3 de Dic. de 2017
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 comentarios
Guillaume
Guillaume el 3 de Dic. de 2017
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 el 3 de Dic. de 2017
Okay, sounds good. I will definitely keep this in mind when asking questions. Thank you.

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 1 de Dic. de 2017
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 comentario
Zach Dunagan
Zach Dunagan el 3 de Dic. de 2017
Editada: Zach Dunagan el 3 de Dic. de 2017
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by