MATLAB: Generating linear array [0 -1 0 1 -2 -1 0 1 2 ……]

for loopMATLAB

Hi
So, I'd like to generate a linear array with output like this
[0 -1 0 1 -2 -1 0 1 2 -3 -2 -1 0 1 2 3 -4 -3 -2 -1 0 1 2 3 4 and so on ]
using for loop. I'm not sure whether to use one or two (nested) for loop
the code should be on the following
n indicates the order and m goes from -n to n with n increases. Another thing, is also the negative indices, this I think can be solved by using a temporary value like temp = 1:length(m). But overall I'm not so sure how should I put everything into the code
for i = 0:n
m = -n:n
.....
end

Best Answer

N = 4;
C = cell(1,N);
for k = 1:N
C{k} = -k:k;
end
V = [0,C{:}]
Giving:
V =
0 -1 0 1 -2 -1 0 1 2 -3 -2 -1 0 1 2 3 -4 -3 -2 -1 0 1 2 3 4