Input value and compare to values in an array
Mostrar comentarios más antiguos
clc, clear, clf
M1=input('What is the Mach number? ');
Mach=[1.050,1.1,1.2,1.3,1.4,1.6,1.8,2,2.5,3,4,5,6,7,8,10];
Maxd=[0.558,1.515,3.944,6.662,9.427,14.652,19.183,22.974,29.797,34.073,38.774,41.118,42,440,43.791,44.429];
So I have this as my code. What I want to do is take an input mach number, find the closest value in the Mach array (rounding down) and then use the corresponding index of the Maxd array for calculations. I'm sure this is simpler than I think it is but I would very much appreciate the help.
Respuesta aceptada
Más respuestas (2)
madhan ravi
el 3 de Nov. de 2018
Editada: madhan ravi
el 3 de Nov. de 2018
M1=input('What is the Mach number? ');
Mach=[1.050,1.1,1.2,1.3,1.4,1.6,1.8,2,2.5,3,4,5,6,7,8,10];
Maxd=[0.558,1.515,3.944,6.662,9.427,14.652,19.183,22.974,29.797,34.073,38.774,41.118,42,440,43.791,44.429];
[~,index]=min(abs(floor((Mach-(M1))))) %floor rounds down towards negative infinity
Maxd(index) %the value to be used in calculations
command window:
>> COMMUNITY
What is the Mach number? 3
index =
10
ans =
34.0730
4 comentarios
Stephan
el 3 de Nov. de 2018
I think your code gives wrong result for input = 3
madhan ravi
el 3 de Nov. de 2018
check now @stephen
Briana Staheli
el 3 de Nov. de 2018
Stephan
el 3 de Nov. de 2018
M1=input('What is the Mach number? ');
Mach=[1.050,1.1,1.2,1.3,1.4,1.6,1.8,2,2.5,3,4,5,6,7,8,10];
Maxd=[0.558,1.515,3.944,6.662,9.427,14.652,19.183,22.974,29.797,34.073,38.774,41.118,42,440,43.791,44.429];
Value = Maxd(find((sort([Mach, M1]))==M1,1,'last')-1)
1 comentario
Briana Staheli
el 3 de Nov. de 2018
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!