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?

 채택된 답변

kintali narendra
kintali narendra 2023년 9월 15일

0 개 추천

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

Amron
Amron 2023년 9월 15일
this is what i got.
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values.
Use the ANY or ALL functions to reduce operands to logical scalar values.
d = 21;
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
r = 2
This runs fine. I tried d = 11, 15, and 21. No error in any case. Is your d not a scalar, but a vector or matrix or some other kind of variable?
Amron
Amron 2023년 9월 15일
i input the 'd' value from editfield.
btw, i don't know whether it is a scalar or a vector. how can i get some informations of it? i mean, how is the way to identify the property of a variable to know it is a scalar or not?
thanks
"how is the way to identify the property of a variable to know it is a scalar or not?"
isscalar(d)
Amron
Amron 2023년 9월 27일
thankyou kintali narendra, your code works. futhermore, in order to solve the issue that i got when i use that code, i use the "spinner" value rather than "edit field". As the result, the value of 'd' is detected as scalar.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Steven Lord
Steven Lord 2023년 9월 15일

0 개 추천

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

Stephen23
Stephen23 2023년 9월 15일
편집: Stephen23 2023년 9월 15일
"Alternately, you could use the discretize function to discretize your data"
Considering the r output values, how would that work?
@Stephen23 like this -
d = randi([5 25],1,8)
d = 1×8
12 18 21 5 11 9 19 17
X = [10.2,13,13.5,20,22.4];
Y = [1.6,1.8,2,Inf];
out = discretize(d,X,Y)
out = 1×8
1.6000 2.0000 Inf NaN 1.6000 NaN 2.0000 2.0000

댓글을 달려면 로그인하십시오.

Stephen23
Stephen23 2023년 9월 15일

0 개 추천

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')
Z = 1×3
1.6000 1.8000 2.0000

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

릴리스

R2022a

태그

질문:

2023년 9월 15일

댓글:

2023년 9월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by