Adding to variable dependant on condition

i have a table and am checking if values lie within this range 60<=a<70, if they do i want to add 1 to another variable "x" so that x will represent the number of values within this range. using an elseif function how would i do this, thanks!
for a = 1:length(x)
if a >= 60 && a <70

6 comentarios

How about just
x = sum(a >= 60 & a < 70)
Holden Earl
Holden Earl el 14 de Abr. de 2020
Editada: Holden Earl el 14 de Abr. de 2020
i have to use an if-elseif statement, and wouldnt that give me the sum of numbers over and 60 and under 70, not the range inbetween?
Holden Earl
Holden Earl el 14 de Abr. de 2020
Editada: Holden Earl el 14 de Abr. de 2020
for a = 1:length(x)
if a >= 60 && a <70
x + 1;
else
end
end
this is what i want to do im just not sure of the syntax?
Tommy
Tommy el 14 de Abr. de 2020
Editada: Tommy el 30 de Jun. de 2020
It would give the number of values which are simultaneously over 60 and under 70, as
a >= 60 & a < 70
would return a logical array, containing a 1 at each value of a between 60 and 70 and a 0 at each other value.
Using an if-else statement would look something like this:
x = 0;
for a = range
if a >= 60 && a < 70
x=x+1
else
% do nothing
end
end
(edit after seeing your above comment) Make sure to reassign x with x+1. Additionally, it seems like x is playing two different roles in your code. If you are going to use
for a = 1:length(x)
then you might want to use a different variable for counting, such as
y=y+1;
Also note the else statement is unnecessary:
for a = 1:length(x)
if a >= 60 && a <70
y=y+1;
end
end
Walter Roberson
Walter Roberson el 14 de Abr. de 2020
You are calculating x+1 and throwing the result away instead of recording the result of the addition.
If a is the vector of values to be checked, you should probably not be using for a . You should probably be using an index into a, like for idx = 1 : length(a) and checking the value of a indexed at that variable.
Holden Earl
Holden Earl el 14 de Abr. de 2020
thats got it, thanks guys!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Abr. de 2020

Editada:

el 30 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by