Check an array of values if within an upper and lower limit

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

 채택된 답변

Dennis
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

Ayman Fathy
Ayman Fathy 2018년 10월 15일
편집: Ayman Fathy 2018년 10월 15일
I basically have an array of 2 columns, first column contains numbers from 0 to 3000 and the second column contains temperature data. I want to store the temperature data in the 3rd column in the same array if the corresponding number in the first column lies between 1000 and 2000 otherwise Nan, I tried your code but it gives me a 3rd column that is the same as the 2nd column
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)
Thanks a lot. It worked!!

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

추가 답변 (1개)

KSSV
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

Ayman Fathy
Ayman Fathy 2018년 10월 15일
편집: Ayman Fathy 2018년 10월 15일
I basically have an array of 2 columns, first column contains numbers from 0 to 3000 and the second column contains temperature data. I want to store the temperature data in the 3rd column in the same array if the corresponding number in the first column lies between 1000 and 2000 otherwise Nan, I tried your code but it gives me a 3rd column that is the same as the 2nd column
Lowerlimit = 1000; Upperlimit = 2000;
if any(s(:,1) >= Lowerlimit) && any(s(:,1) <= Upperlimit) s(:,3) = s(:,2); else s(:,3) = NaN; end
%Itried this code but didnt work
this was my code previously and again didnt work:
% for x = size(s, 1) % % if (s(x, 1) >=Lowerlimit) (s(x,1) <= Upperlimit) % s(x, 3) = s(x, 2); % else % s(x, 2) = NaN; % end % end

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

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2018년 10월 15일

댓글:

2018년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by