How to make sure all the elements in an array are less than certain value?

I have an array of type double (name= speed) and it has 72000 values, and i want to write a condition that if any of the elements in speed are less than '800' then do certain calculations, if it is greater than '800' then do certain caluclations

 Respuesta aceptada

Image Analyst
Image Analyst el 18 de Dic. de 2018
Editada: James Tursa el 19 de Dic. de 2018
Let's say you want to multiply by 2 if less than 800, and divide by 4 otherwise. Create a mask, then do the assignment of the new values.
mask = speed < 800;
speed(mask) = speed(mask) * 2;
% ~mask (tilde mask) inverts mask and selects speed >= 800
speed(~mask) = speed(~mask) / 4; % fixed typo

4 comentarios

Good answer, I may have misinterpreted the question because I thought he was trying to check to see if elements any were less than 800 then do cirtain things to the whole array not just the elements that were less. In that case I was going to suggest making a mask and using the "all" or "any" functions.
Typo:
speed(~mask) = speed(~mask) / 4; % you need the ~mask in both places
Thank you, @Image Analyst,Elijah,James

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by