What is the shortest way to get
x =
2 -2 4 -4 6 -6 ... 14 -14 16 -16
I figured out two options, one of them somewhat short:
>>x=[];
for k=2:2:16
x=[x k -k];
end
x
However, I wonder if it's possible to affect size and direction of the s in a:s:b, since the sequence above can be constructed with a regular pattern: +2, -4, +6, -8, +10 etc. That would be even shorter.
A two-line solution without a loop:
xt = [2:2:16; -(2:2:16)]; x = xt(:)'xtis 2 by 8. Since the(:)operation outputs it as a single column vector by concatenating each successive column ofxt, the desired result is obtained by transposing.