4

This is an extension of this question.

I have xyPairsAll, which is a matrix holding sublists. It looks like

{...
{{1., 1812.}, {2., 10076.}, {3., 4764}, {1., 3475.}, {2., 3572.}, {3., 3985.}}, 
{{1., 6839.}, {2., 3849.}, {3., 2746}, {1., 3578.}, {2., 5629.}, {3., 3849.}},  
{{1., 6839.}, {2., 3849.}, {3., 2746}, {1., 10092.}, {2., 1638.}, {3., 3728.}}
...}

I want to split each sublist into 1., 2., 3. sub-sublists based on the first {x, y} pair of each sublist. I don't want to merge any of the sublists; the resulting matrix will gain one more level. It should look like

{...
{{{1., 1812.}, {2., 10076.}, {3., 4764}},   
 {{1., 3475.}, {2., 3572.}, {3., 3985.}}}, 
{{{1., 6839.}, {2., 3849.}, {3., 2746}},    
 {{1., 3578.}, {2., 5629.}, {3., 3849.}}}, 
{{{1., 6839.}, {2., 3849.}, {3., 2746}},    
 {{1., 10092.}, {2., 1638.}, {3., 3728.}}}
...}

I can do the split operation on the first sublist with

Split[xyPairsAll[[1]], (First[#2] > First[#1]) &]

How can I repeat the same split operation on all the other sublists too, without merging them?

brienna
  • 1,071
  • 5
  • 12
  • 2
    Split[#, ((First[#2] > First[#1])&)]& /@ xyPairsAll? – eyorble Jul 08 '18 at 18:17
  • Ahhhhh. It works. I had tried that with xyPairsAll[#]] instead of just #. Not sure why, because that doesn't even make sense. Thanks, @eyorble – brienna Jul 08 '18 at 18:18

2 Answers2

2
Partition[#, 3] & /@ xyPairsAll
1

The structural modification seems to be the same in all rows, so I think ArrayReshape will work:

L = {
     {{1., 1812.}, {2., 10076.}, {3., 4764}, {1., 3475.}, {2., 3572.}, {3., 3985.}}, 
     {{1., 6839.}, {2., 3849.}, {3., 2746}, {1., 3578.}, {2., 5629.}, {3., 3849.}},  
     {{1., 6839.}, {2., 3849.}, {3., 2746}, {1., 10092.}, {2., 1638.}, {3., 3728.}}
    };

With[{dim = Dimensions[L], max = Round[Max[L[[1, All, 1]]]]},
 ArrayReshape[L, ReplacePart[dim, 2 -> Sequence[dim[[2]]/max, max]]]]
{
 {{{1., 1812.}, {2., 10076.}, {3., 4764}}, {{1., 3475.}, {2., 3572.}, {3., 3985.}}},
 {{{1., 6839.}, {2., 3849.}, {3., 2746}}, {{1., 3578.}, {2., 5629.}, {3., 3849.}}},
 {{{1., 6839.}, {2., 3849.}, {3., 2746}}, {{1., 10092.}, {2., 1638.}, {3., 3728.}}}
}
Coolwater
  • 20,257
  • 3
  • 35
  • 64