0

Given an array say of 1 dimension, {{x1,y1,z1},{x2,y2,z2}…{x100,y100,z100}}, is there a clean way to keep the array contained and yet convert it into a 3d array based on an interval? Such as {{{x1,y1,z1}…{x20,y20,z20}},{{x21,y21,z21}…and so on...}}}


list1={1,2,3,4,5,6,7,8,9,10,11,12}

Goal:

listgoal={{{1,2,3},{4,5,6}},{{7,8,9},{10,11,12}}}
BOUNCE
  • 589
  • 2
  • 9

1 Answers1

3
list1 = Range[12];
ArrayReshape[list1, {Length[list1]/6, 2, 3}]

{{{1, 2, 3}, {4, 5, 6}}, {{7, 8, 9}, {10, 11, 12}}}

ArrayReshape converts an input list or array into an array of the specified dimensions. If you don't specify the first dimension to be Length[list1]/6, it'll drop everything after the first group of 6. This, of course, assumes that the length of the list can be assumed to be a multiple of 6. The 6 should be changed if the other dimensions should changed as well -- it's just accounting for the size of the inner group.

eyorble
  • 9,383
  • 1
  • 23
  • 37
  • Figured this was probably a duplicate just after writing this, but it answered the question quickly. Win some and lose some as far as the house-keeping is concerned. – eyorble Apr 03 '19 at 15:37
  • I feel as if your answer is more thorough/less hard-coded than the answers to the linked problem... I am fine with this question being marked as a duplicate, but if you wish to proceed with that I recommend you putting your answer on that question (particularly the paragraph). Thanks! – BOUNCE Apr 03 '19 at 16:15