MATLAB: Loop to count amount sin(x)>0.5 when 1>x>1000

for loopif statementwhile loop

What is wrong?
for n=1:1000
while n<1000
if sin(n)>0.5
s(n)=sin(n);
n=n+1;
else
n=n+1;
end
end
end
L=length(s);
disp(L)

Best Answer

To get what you want loop is not required.
x = linspace(1,1000,10^5) ;
y = sin(x) ;
nnz(y>0.5)
If you want loop:
% loop
thesum = 0 ;
for i = 1:length(x)
y = sin(x(i)) ;
if y > 0.5
thesum = thesum+1 ;
end
end
thesum