Overflow an integer sequence at a given value

51 Views Asked by At

Sorry, the question seems to be extremely simple, but I fail to find an answer. I have an integer sequence

1, 2, 3, ... 8

I would like to convert it to

1, 2, 3, 4, 1, 2, 3, 4

What mathematical operation would give me this result?

An obvious solution is mod: (in Matlab)

a = mod(1:8, 4)

but it of course results in

1, 2, 3, 0, 1, 2, 3, 0.
1

There are 1 best solutions below

5
On BEST ANSWER

You want to do

a = mod([1:8]-1,4)+1

or in Python

(numpy.arange(8) % 4)+1