I was modifying "for" loop implementation in Freemat and realized that neither Freemat 3.5 nor Octave 3.0 match the behavior of "for" loops in matlab.
If you run the script below you will see that the first loop performs 20 iterations in matlab, but only 19 in Octave. "Loop 2" below performs 20 iterations in both systems.
For the second test you should change "if 1" to "if 0". Plot "for loop vs. my implementation" In matlab is constant zero. However, in Octave the plot shows the difference of more than 20*eps.
A side note, in matlab "for loop vs. my implementation" is constant zero, but "vectorized vs. my implementation" shows difference of up to 4*eps. This means that "k=[first:step:last]" and "for k=first:step:last" use different algorithms and produce different results!!!!
Compatible "for" loop implementation will go in the next release of Freemat, and Freemat already has almost correct implementation of "k=[first:step:last]" construct (a fairly complicated algorithm).
% loop 1 % matlab for loop output n1=0; for k=first1:step1:last1 mat_k(n1+1)=k;
n1=n1+1; end
% loop 2 % matching internal implementation nst = round((last1-first1)/step1)+ 1; %precompute number of steps for n = 0:(nst-1) my_k(n+1)=first1+n*step1; end [ length(my_k) length(mat_k) ]
dif = my_k-mat_k;
k = [first1:step1:last1]; dif1 = my_k - k; subplot(1,2,1);plot(dif./eps); title('for loop vs. my implementation');xlabel('step');ylabel('difference / eps') subplot(1,2,2);plot(dif1./eps); title('vectorized vs. implementation');xlabel('step');ylabel('difference / eps')