Multiple conditions in an IF statement and they are not logical scalar values
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matt Stone
am 25 Feb. 2021
Bearbeitet: KALYAN ACHARJYA
am 25 Feb. 2021
Here is the sample code that I have written and keep getting this error:
n=input('Enter the values: ');
xmax=input('Enter the Maximum: ');
xmin=input('Enter the Minimum: ');
delx=(xmax-xmin)/(n-1);
for i=1:n
x(i)=xmin-(i-1)*delx;
if ((x>=0) && (x<=2))
m(i)=20*x-4.5*x^2;
elseif ((x>=2) && (x<=4))
m(i)=20*x(i)-18*(x(i)-1);
else
m(i)=20*x-30-18*(x(i)-1);
end
end
plot(x,m)
The Matlab output is below:
>> PracticeExamME1905
Enter the values: 101
Enter the Maximum: 6
Enter the Minimum: 0
Operands to the || and && operators must be
convertible to logical scalar values.
Error in PracticeExamME1905 (line 7)
if ((x>=0) && (x<=2))
0 Kommentare
Akzeptierte Antwort
KALYAN ACHARJYA
am 25 Feb. 2021
Bearbeitet: KALYAN ACHARJYA
am 25 Feb. 2021
Replace x with x(i) in all data, then code execute without any error.
More: read about preallocation and logical indexing (MATLAB)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!