3
$\begingroup$

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?

$\endgroup$
  • 2
    $\begingroup$ Split[#, ((First[#2] > First[#1])&)]& /@ xyPairsAll? $\endgroup$ – eyorble Jul 8 '18 at 18:17
  • $\begingroup$ Ahhhhh. It works. I had tried that with xyPairsAll[#]] instead of just #. Not sure why, because that doesn't even make sense. Thanks, @eyorble $\endgroup$ – briennakh Jul 8 '18 at 18:18
1
$\begingroup$
Partition[#, 3] & /@ xyPairsAll
$\endgroup$
0
$\begingroup$

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.}}}
}
$\endgroup$

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.