Calling max value of variable from another function
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have made a function named "example.m" in which I have created two variables "x" and "y" both have certain range.
function [x,y,T] = example(v,sigma,G,N,K)
x=0:0.01:2;
y=(G*N^2*x/sigma)-(K+v);
end
Now in another function, named "loadvalues.m" I need to call the maximum value of "y" along with the corresponding value of "x" both of them are loaded in another variable named "loady" and "loadx" respectively. Can someone please tell me how to do this?
1 commentaire
Réponse acceptée
dpb
le 13 Août 2017
The y in the function example has nothing to do with the y from the load y command so the question is unclear as to why you're referring to the latter...unless there's code not shown which SAVE d the x and y after you ran the function and you used the intermediary from the disk to get the output of the function later on instead of just saving the results of running the function itself???
If that's the case, IA's Answer is spot, but why not save the intermediary step and write
[x,y,T] = example(v,sigma,G,N,K);
[maxy,iy]=max(y);
maxx=x(iy);
?
As to the second question, there is only one value of T returned and it will correspond to whichever condition is T in the IF block; the other branch will never be evaluated. If it's important as to which condition is so, you could add another output which is a flag variable that has the value of true/false or 0/1 or -1/+1, whatever set of values you wish to indicate that condition.
4 commentaires
dpb
le 15 Août 2017
...
u=0:0.01:5;
w=2.5;
if u<w
...
The if statement above in Matlab does NOT do what you think it does (or what you want); in fact it will never execute the true clause.
In
if == expression
the condition is true iff ALL (real) elements of the expression are each true; the IF block is not a looping construct; it is a simple logic test only.
Also, T will be a vector of length(u) as written.
It's not clear exactly what you do want; do you want a vector T of all values with the evaluation of the expression for each element based on the ratio or only those in the T block or a single value that is the value that's the first location that U<W or what, precisely?
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!