3

It's got to be easy, but I can't come up with a general solution.

m = {{16, 2, 3, 13}, {{5, 11, 10, 8}, {9, 7, 6, 12}}, {4, 14, 15, 1}};

What I'm trying to obtain:

m = {16, 2, 3, 13}, {5, 11, 10, 8}, {9, 7, 6, 12}, {4, 14, 15, 1}};

These 'groups of rows' can show at many places, so FlattenAt solution is not enough. I'd really like to use a Flatten or an ArrayFlatten solution.

Literal
  • 383
  • 1
  • 10

3 Answers3

3
mat = {{{16, 2, 3, 13}, {5, 11, 10, 8}}, {9, 7, 6, 12}, {4, 14, 15, 1},
       {{a, b, c}, {v, x, y}}};

mat2 = MapAt[## & @@ # &, mat, Position[mat, {{__}, {__}}]]
(* {{16, 2, 3, 13}, {5, 11, 10, 8}, {9, 7, 6, 12}, {4, 14, 15, 1}, 
    {a, b, c}, {v, x, y}} *)

mat3 = FlattenAt[mat, Position[mat, {{__}, {__}}]]
(* {{16, 2, 3, 13}, {5, 11, 10, 8}, {9, 7, 6, 12}, {4, 14, 15, 1}, 
    {a, b, c}, {v, x, y}} *)
kglr
  • 394,356
  • 18
  • 477
  • 896
  • Ok thanks. I thought it would have been easier to achieve it (involving a single function) – Literal Apr 22 '15 at 22:07
  • I swear, BlankNullSequence must have compromising photos of you, as much as you use it ;-) +1 for patterny prettiness! – ciao Apr 22 '15 at 22:17
  • thank you @rasher. Source: BlankNullSequence infection :) – kglr Apr 22 '15 at 23:09
  • Mr. 70K, please try a little harder to help me find duplicates; I know you have seen this one. You're #2 on the site now and I need all the help I can get! – Mr.Wizard Apr 22 '15 at 23:35
  • Heard you @Mr.W. What keywords did you use to get the link (this one)? – kglr Apr 23 '15 at 08:07
  • I believe I searched for Partition and Flatten, remembering an earlier answer with that solution. Admittedly this was based on an answer that was posted after yours. Rereading my comment from yesterday it sounds like I was scolding you and that was surely not my intent. Rather I fear that I see the site becoming increasingly disorganized as the total number of questions grows and an increasing amount of effort spent on answering questions that have already been answered. I'm not sure how to help that and it makes me a little agitated. Sorry. – Mr.Wizard Apr 23 '15 at 17:10
  • @Mr.Wizard, not to worry -- I didn't take it that way. I do share your concern .Many questions trigger a seen-that-somewhere feeling, and finding a relevant link is getting increasingly more difficult - sometimes I spend hours searching for my own answers, and often without success. I agree we need more effort to search but without accompanied efforts to make the search engine more effective it will be difficult to avoid the vicious spiral. – kglr Apr 23 '15 at 17:37
3

If the "Rows" are all same length (here 4):

m = {{16, 2, 3, 13}, {{5, 11, 10, 8}, {9, 7, 6, 12}}, {4, 14, 15, 1}};

Partition[Flatten[m], 4]

(* {{16, 2, 3, 13}, {5, 11, 10, 8}, {9, 7, 6, 12}, {4, 14, 15, 1}} *)

Will be much more efficient...

ciao
  • 25,774
  • 2
  • 58
  • 139
1

Given:

m = {{16, 2, 3, 13}, {{5, 11, 10, 8}, {9, 7, 6, 12}}, {4, 14, 15, 1}};

We could use Sequence to "unwrap" lists of depth 3:

Apply[Sequence, m, {-3}]

(* {{16, 2, 3, 13}, {5, 11, 10, 8}, {9, 7, 6, 12}, {4, 14, 15, 1}} *)

Equivalently:

Apply[##&, m, {-3}]

Direct replacement of nested lists can work too:

Replace[m, {x:_List...} :> x, {1}]

These approaches will work on variable-length rows:

m2 = {{1, 2}, {{3, 4, 5}, {6, 7, 8}}, {9, 10, 11}, {{12, 13}, {14, 15}, {16}}};

Apply[##&, m2, {-3}]

(* {{1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}, {12, 13}, {14, 15}, {16}} *)

If the nesting is variable-depth, we can use Reap and Sow:

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

Scan[Sow, m3, {-2}] // Reap // #[[2, 1]] &

(* {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}} *)
WReach
  • 68,832
  • 4
  • 164
  • 269