1
$\begingroup$

I would like to select elements from 2D list according to variable position (i.e. stride). For example:

x=Range@25 ~Partition~ 5

I want a command to select elements with stride t=2 as y={{1,3,5},{11,13,15},{21,23,25}}. Simply, take one and leave one. What if t=3,4,...etc?

Thanks

$\endgroup$
2
$\begingroup$
t = 2;
Partition[Range[25], 5][[;; ;; t, ;; ;; t]]

{{1, 3, 5}, {11, 13, 15}, {21, 23, 25}}

Or, as suggested by J.M.issomewhatokey, use Take:

Take[Partition[Range[25], 5], {1, -1, t}, {1, -1, t}]

{{1, 3, 5}, {11, 13, 15}, {21, 23, 25}}

Partition[Range[25], 5][[;; ;; 3, ;; ;; 3]]

{{1, 4}, {16, 19}}

$\endgroup$
  • $\begingroup$ Take[] ought to work here, too. $\endgroup$ – J. M. is away Oct 12 '18 at 8:03
  • $\begingroup$ @J.M. yes it does. Thank, you. $\endgroup$ – kglr Oct 12 '18 at 8:05
  • $\begingroup$ Thanks :) it works well... $\endgroup$ – Alien Tsukuba Oct 12 '18 at 8:11
  • $\begingroup$ @AlienTsukuba, you are most welcome. $\endgroup$ – kglr Oct 12 '18 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.