Check an array of values if within an upper and lower limit
39 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ayman Fathy
am 15 Okt. 2018
Kommentiert: Ayman Fathy
am 15 Okt. 2018
I am trying to do the same thing as the following example but checking an array of numbers and not just x. However it is not working. can someone help?
x = 10;
minVal = 2;
maxVal = 6;
if (x >= minVal) && (x <= maxVal)
disp('Value within specified range.')
elseif (x > maxVal)
disp('Value exceeds maximum value.')
else
disp('Value is below minimum value.')
end
0 Kommentare
Akzeptierte Antwort
Dennis
am 15 Okt. 2018
If you want to operate on arrays you could do it like this:
x = 1:10;
minVal = 2;
maxVal = 6;
x(x(x<=maxVal)>=minVal)
disp('Values within specified range.')
3 Kommentare
Dennis
am 15 Okt. 2018
x = [randi(10,19,1),randi(100,19,1)];
minVal = 2;
maxVal = 6;
x(:,3)=NaN;
idx=find(x(:,1)>=minVal&x(:,1)<=maxVal);
x(idx,3)=x(idx,2)
Weitere Antworten (1)
KSSV
am 15 Okt. 2018
minVal = 2;
maxVal = 6;
if any(x >= minVal) && any(x <= maxVal)
disp('Value within specified range.')
elseif any(x > maxVal)
disp('Value exceeds maximum value.')
else
disp('Value is below minimum value.')
end
3 Kommentare
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices 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!