Check an array of values if within an upper and lower limit
조회 수: 39 (최근 30일)
이전 댓글 표시
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
채택된 답변
Dennis
2018년 10월 15일
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
Dennis
2018년 10월 15일
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)
추가 답변 (1개)
KSSV
2018년 10월 15일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!