## Flattening a nested matrix

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

### Flattening a nested matrix

If I create the matrix "mat" as shown below, how can I remove the nesting so that I have a flat 6×8 matrix with all the 1's and 0's still in their original positions?

pat←2 2⍴1 1 1 0
pat
1 1
1 0
mat←3 4⍴⊂pat
mat
1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0
1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0
1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0

desired result:
1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0
1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0
1 1 1 1 1 1 1 1
1 0 1 0 1 0 1 0
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

### Re: Flattening a nested matrix

⊃⍪/,/mat
6 8⍴↑,/mat
Roger|Dyalog

Posts: 215
Joined: Thu Jul 28, 2011 10:53 am

### Re: Flattening a nested matrix

⍎⍤1⍕mat
Roger|Dyalog

Posts: 215
Joined: Thu Jul 28, 2011 10:53 am

### Re: Flattening a nested matrix

`      6 8⍴0 2 1 3⍉↑mat`

Phil Last

Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm

### Re: Flattening a nested matrix

`      6 8⍴0 2 1 3⍉↑mat`
can be generalised to
`      ,[0 1],[2 3]0 2 1 3⍉↑mat`
but this merely shows the weakness of ravel with axis. If its argument (axis) took a similar form to that of slicing transpose but with the semantic that the data were accumulated rather than selected, we could do the whole thing including the transpose in a single operation.
`      ,[0 1 0 1]↑mat`
But this in turn shows up the general weakness and ad-hoc nature of axis specification wherein the meaning of the axis is different for every function. If it were truly implemented as an operator with a different glyph and following proper operator syntax it could be made comprehensible, comprehensive and predictable.

Phil Last

Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm

### Re: Flattening a nested matrix

Thanks Roger and Phil!

"comprehensible, comprehensive and predictable" would be very nice, but I'm just happy to get a solution to my problem. Thanks!

-Stu
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

### Re: Flattening a nested matrix

How can I do the inverse of flattening? I know the dimensions of the flattened matrix and the dimensions of the submatrices that I want. I know this probably involves ⍉, ⍴, and ravel, but I can't seem to put the pieces together correctly.

1 0 0 1 1 0 0 1
0 1 1 0 -> 0 1 1 0
1 1 1 1
0 0 0 1 1 1 1 1
0 0 0 1
Stu

Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

### Re: Flattening a nested matrix

It isn't clear exactly what you do want.
Can you make one up somehow and display it?
`      z←...whatever...      ]display z`

Phil Last

Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm

### Re: Flattening a nested matrix

To answer my own question were you trying to do something like:
`      R C←4 6                                         r c←2 3                                         ]display R C⍴⍳99                          ┌→────────────────┐                             ↓ 0  1  2  3  4  5│                             │ 6  7  8  9 10 11│                             │12 13 14 15 16 17│                             │18 19 20 21 22 23│                             └~────────────────┘                                   ]display ⍉↑(⊂R⍴r↑1)⊂[0]¨(C⍴c↑1)⊂[1]R C⍴⍳99┌→──────────────────────┐                       ↓ ┌→────┐    ┌→──────┐  │                       │ ↓0 1 2│    ↓3  4  5│  │                       │ │6 7 8│    │9 10 11│  │                       │ └~────┘    └~──────┘  │                       │ ┌→───────┐ ┌→───────┐ │                       │ ↓12 13 14│ ↓15 16 17│ │                       │ │18 19 20│ │21 22 23│ │                       │ └~───────┘ └~───────┘ │                       └∊──────────────────────┘                                                                       `
⎕io←⎕ml←0

Phil Last

Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm

### Re: Flattening a nested matrix

Best I can do in a short while
`      ⎕cr'tessellate' tessellate←{⎕IO←⎕ML←0     R←⍴⍵     k←≢R     r←1+(-k)↑¯1+⍺     s←⌈R÷r     R←r×s     ⊂[1+2×⍳k](,s,⍪r)⍴R↑⍵⍝ ⍺ shape of sub arrays⍝ ⍵ multi-d array⍝ ← nested array of ⍺ shaped subarrays of ⍵⍝   if ⍺ is shorter than rank ⍵ it is padded at left with ones.⍝   if any ⍺ is not a factor of ⍴⍵, ⍵ is padded to make it so. }      ]display 2 3 tessellate 5 7⍴⍳99 ┌→───────────────────────────────┐↓ ┌→────┐    ┌→───────┐ ┌→─────┐ ││ ↓0 1 2│    ↓ 3  4  5│ ↓ 6 0 0│ ││ │7 8 9│    │10 11 12│ │13 0 0│ ││ └~────┘    └~───────┘ └~─────┘ ││ ┌→───────┐ ┌→───────┐ ┌→─────┐ ││ ↓14 15 16│ ↓17 18 19│ ↓20 0 0│ ││ │21 22 23│ │24 25 26│ │27 0 0│ ││ └~───────┘ └~───────┘ └~─────┘ ││ ┌→───────┐ ┌→───────┐ ┌→─────┐ ││ ↓28 29 30│ ↓31 32 33│ ↓34 0 0│ ││ │ 0  0  0│ │ 0  0  0│ │ 0 0 0│ ││ └~───────┘ └~───────┘ └~─────┘ │└∊───────────────────────────────┘`

Phil Last

Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm

Next

### Who is online

Users browsing this forum: No registered users and 1 guest