Here is the solution
for i = 2:100
[Ricp Ticp ER weight] = icp(M, D, i);
% Transform data-matrix using ICP result
Dicp = Ricp * D + repmat(Ticp, 1, n);
set(0, 'CurrentFigure', f1)
ER1 = ER(end-1) - ER(end)
if ER1 < 0.005
break
end
end
%% this is the point where i need to add something:
%% If ER1 < 0.005 it has to continue outside the i loop.