MATLAB: Recurring Sequences of Numbers

declarationvector

Is there a way to declare v as a vector of numbers without a stopping point [0 -1 0 1 0 -1 0 1 … n]? And from that I want to be able to use the number a to get all the entries of vector v. For example: a = 4 v = [0 -1 0 1]

Best Answer

>> fun = @(N)round(cos((1:N)*pi/2));
>> fun(10)
ans =
0 -1 0 1 0 -1 0 1 0 -1
>> fun(4)
ans =
0 -1 0 1