You get a warning in each iteration:
Warning: Integer operands are required for colon operator when used as index
var1 = 0.1;
vec = 1:1000;
length_var = 1/(var1 * 0.1);
for ii=1:20
a = round(ii*(0.25*length_var));
b = a - 1 + 3*length_var;
fprintf('%.16g\n', b - a);
c(ii,:) = vec(a:b);
end
In the 9th iteration b-a becomes 299. Then vec(a:b) has 300 elements, but c(ii,:) has 299 only.
Do not use floating point numbers as indices. You have applied a round for the index a already. Do this for b also. Or even better: Let length_var have an integer value:
fprintf('%.16g', 1/(var1 * 0.1))
99.99999999999999
This is an expected effect when working with floating point numbers stored with limited precision. To get more explanations search in this forum or in the net for "IEEE754".
Best Answer