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 个评论

How about just
x = sum(a >= 60 & a < 70)
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?
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?
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
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.
thats got it, thanks guys!

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

编辑:

2020-6-30

Community Treasure Hunt

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

Start Hunting!

Translated by