I want to make a list of many (say 1000) test instances for a geometry problem. The problem involves two line segments and a point. So I need
{
(* first line *) (*second line*) (*point*)
{ {{x0,y0},{x1,y1}} , {{x2,y2},{x3,y3}} , {x4,y4} },
... (*1000 times*)
}
Now this is quite easy to achieve with
{#[[{1, 2}]], #[[{3, 4}]], #[[5]]} & /@
RandomReal[{-100, 100}, {100, 5, 2}]
The Dimensions
of that desired array are {1000,3,2}
, which ignores the fact that the lines have extra depth while the point does not.
Is there a way to make this reshaping happen with ArrayReshape
, or is it for strictly rectangular reshaping? Is there a clever way to use other functions like Thread
, Transpose
etc with their powerful options to do this?
I'll do some benchmarking for curiosity's sake.
I recently asked a similar question Thread Matrices, like image channels. With my wording, the most direct answer (restricted to Thread
), isn't the best -- it's good to see plenty of votes for all the answers.
Dimensions
instead of{1000,3,2}
? $\endgroup$Dimensions
; I'm not looking for a generalizedDimensions
function. I suppose I meant other functions in conjunction withArrayReshape
, smarter than simple mapping hacks (i.e. producing 1000 elements of the form{{{x0,y0},{x1,y1}},{{x2,y2},{x3,y3}},{{x4,y4}}}
and then Mapping to turn the{{x4,y4}}
's into{x4,y4}
's). $\endgroup${{#, #2}, {#3, #4}, #5} & @@@ list
andTranspose[{#[[All, {1, 2}]], #[[All, {3, 4}]], #[[All, 5]]}] &@list
wherelist
is your input list with dimensions{100,5,2}
$\endgroup$