Array rotated/transposed vertically

hey list,

is there a method to rotate an array vertically? given a boundary and a step value, I would like to rotate/transpose values of an array. For example: [0, 1, 2, 3] transposed by a step of value 1 within boundaries [0,5] would result in a new array [1, 2, 3, 4] at first iteration, [2, 3, 4, 5] at the next, and [3, 4, 5, 0] following one - values fold back once reaching the upper boundary.

thanks for your help

m

You can put

.mod(6)

after the array.

The documentation isn’t very clear under SimpleNumber if you’re unfamiliar with modular arithmetic, but it basically divides by the number in parenthesis and returns the remainder.

I’m assuming you have a system for the transposition part of this, but if not, check out .do and .collect.

thanks, I think I got it working:

~step = 1;
~limit = 5;
~a = [ 0, 1, 2, 3];
~b = ~a.collect{|item| item + ~step}.mod(~limit + 1); ~a = ~b.copy; 
2 Likes

ok. I have encountered a problem.

I want to be able to set the lower boundary too. For example, if lower boundary is -1 and upper 3 then an array [0,1,2,3] transposed/folded by step 1, would return [1,2,3,-1], [2,3,-1,0] … and so on. Any tips on a solution for that?

You can use

.wrap(-1, 3) \\first arg is lower limit, second is upper

So:

(
x = (0..20); \\x returns [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

x.wrap(0,6); \\ returns [ 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6 ]
)

Excellent!!!

.wrap does the job

thanks for your help @mjsyts

all the best

marcin

1 Like