MATLAB: Simplifying a for loop

for loopMATLAB

Hello, I'm trying to approximate the Feigenbaum constant by using the logistic map. I came up with a script that gives you the bifurcation parameters, as below:
for k=nl:nh
if abs(x(k,nit+1)-x(k,nit+1-2))<e
b(2)=a(k);
elseif abs(x(k,nit+1)-x(k,nit+1-4))<e
b(3)=a(k);
elseif abs(x(k,nit+1)-x(k,nit+1-8))<e
b(4)=a(k);
elseif abs(x(k,nit+1)-x(k,nit+1-16))<e
b(5)=a(k);
elseif abs(x(k,nit+1)-x(k,nit+1-32))<e
b(6)=a(k);
elseif abs(x(k,nit+1)-x(k,nit+1-64))<e
b(7)=a(k);
elseif abs(x(k,nit+1)-x(k,nit+1-128))<e
b(8)=a(k);
elseif abs(x(k,nit+1)-x(k,nit+1-256))<e
b(9)=a(k);
break
end
end
('nit' means number of iterations, and k is related to the parameter a (r in some references))
Now it seems obvious that there is a simpler representation of the above code, as the integers are just the first eight exponents of 2. But I just can't come up with one. Could somebody help me?

Best Answer

Assuming that e is a scalar, you could create the index for b using a vector, something like this:
for k = ...
idb = nnz(abs(x(k,nit+1)-x(k,nit+1-pow2(1:8)))<e)
b(idb) = a(k)
end
You will need to add some special conditions for zero, one, and for that break statement.