How to take a value between two values
53 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Amron
le 15 Sep 2023
Commenté : Dyuman Joshi
le 27 Sep 2023
hello, please help me.
i couldn't use "if" statement to take 'r' value. The 'r' value depends on 'd' value. but i don't know how to make the code for this.
lets take an example. when i input 'd' value as 15, the 'r' value that i got is 1.6 instead of 1.8. what should i do?![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1483161/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1483161/image.png)
0 commentaires
Réponse acceptée
kintali narendra
le 15 Sep 2023
make small changes in the if statement. The code given below is the right way to use if.
if (10.2 <= d) && (d < 13)
r = 1.6
elseif (13.5 <= d) && (d < 20.0)
r = 1.8
elseif (20.0 <= d) && (d < 22.4)
r = 2.0
end
5 commentaires
Stephen23
le 15 Sep 2023
"how is the way to identify the property of a variable to know it is a scalar or not?"
isscalar(d)
Plus de réponses (2)
Steven Lord
le 15 Sep 2023
If you hover over the underlined <= operators in the code in the MATLAB Editor, you will see a Code Analyzer message explaining why that code doesn't do what you think it does and suggest how you can modify the code to do what you likely want to do.
Alternately, you could use the discretize function to discretize your data without a (potentially lengthy) if / elseif / elseif / elseif ... / end statement.
2 commentaires
Dyuman Joshi
le 27 Sep 2023
@Stephen23 like this -
d = randi([5 25],1,8)
X = [10.2,13,13.5,20,22.4];
Y = [1.6,1.8,2,Inf];
out = discretize(d,X,Y)
Stephen23
le 15 Sep 2023
d = [11,15,21];
X = [10.2,13.5,20,22.4];
Y = [1.6,1.8,2,Inf];
Z = interp1(X,Y,d, 'previous')
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!