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

38 visualizaciones (últimos 30 días)
Ayman Fathy
Ayman Fathy el 15 de Oct. de 2018
Comentada: Ayman Fathy el 15 de Oct. de 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

Respuesta aceptada

Dennis
Dennis el 15 de Oct. de 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 comentarios
Dennis
Dennis el 15 de Oct. de 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)
Ayman Fathy
Ayman Fathy el 15 de Oct. de 2018
Thanks a lot. It worked!!

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 15 de Oct. de 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 comentarios
Ayman Fathy
Ayman Fathy el 15 de Oct. de 2018
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
Ayman Fathy
Ayman Fathy el 15 de Oct. de 2018
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

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by