Recurring Sequences of Numbers

1 view (last 30 days)
C
C on 1 Oct 2017
Commented: Stephen23 on 1 Oct 2017
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]

Accepted Answer

Stephen23
Stephen23 on 1 Oct 2017
>> 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
  2 Comments
C
C on 1 Oct 2017
What would the function look like to do the same for [0 1 0 -1 ... n]? I've tried using the fun function with sin instead of cos, but the first value is 1 instead of 0.
Stephen23
Stephen23 on 1 Oct 2017
You need to also offset the range of input values, e.g.:
(0:N-1)

Sign in to comment.

More Answers (0)

Categories

Find more on Modeling in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!