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)
Holden Earl
Holden Earl 2020년 4월 14일
편집: Holden Earl 2020년 4월 14일
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 2020년 4월 14일
편집: Holden Earl 2020년 4월 14일
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 2020년 4월 14일
편집: Tommy 2020년 6월 30일
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.
Holden Earl
Holden Earl 2020년 4월 14일
thats got it, thanks guys!

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

답변 (0개)

카테고리

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

태그

질문:

2020년 4월 14일

편집:

2020년 6월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by