MATLAB: How can i automate the following process for n times

automatic;MATLABrepeat

Hi everyone, how can i automate the following process for n times?
Thank you so much!
r=0.002
POINT1= [X, Y, Z];
POINT2= [POINT1(:,1), POINT1(:,2), r+POINT1(:,3)];
POINT3= [POINT2(:,1), POINT2(:,2), r+POINT2(:,3)];
POINTn= [POINTn-1(:,1), POINTn-1(:,2), r+POINTn-1(:,3)]

Best Answer

Never create numbered variables. It's a clear indication that you should be using an array, which can be easily indexed instead.
r = 0.002;
X = [0;1;2]; Y = [0;2;4]; Z = [0;-1;1];
n = 5;
Point = [X, Y, Z] + [0, 0, 1] .* (permute(0:n-1, [1, 3, 2]) * r)
Your is now Point(:, :, i).
The above is just one of many ways of generating that 3d matrix.