Transpose ragged array

General APL language issues

Transpose ragged array

Postby paulmansour on Mon Jul 24, 2017 6:04 pm

Hi,

I'm trying to transpose a vector of different length vectors, essentially a ragged matrix. The only way I can think of to do it is to introduce a special value which is subsequently removed:

      transpose←{
n←≢¨⍵
m←⌈/n
f←⎕NULL
m←↓⍉⊃⍵,¨(m-n)⍴¨f
m~¨f
}


      ]disp  transpose (1 2 3) (4 5) (6 0 7 8 9)


┌→────┬─────┬───┬─┬─┐
│1 4 6│2 5 0│3 7│8│9│
└~───→┴~───→┴~─→┴→┴→┘



Is there a better way to this? Perhaps a creative use of indexing?
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: Transpose ragged array

Postby JohnS|Dyalog on Tue Jul 25, 2017 7:46 am

One alternative might be to use a "transposed" vector of masks:

      transpose2←{⎕ml←1
m←↓⍉↑1⊣¨¨⍵
m/¨↓⍉↑⍵
}

      transpose2 (1 2 3)(4 5) (6 0 7 8 9)
┌─────┬─────┬───┬─┬─┐
│1 4 6│2 5 0│3 7│8│9│
└─────┴─────┴───┴─┴─┘
JohnS|Dyalog
 

Re: Transpose ragged array

Postby paulmansour on Sat Jul 29, 2017 5:05 pm

Thanks John.

It's seems to be a surprisingly complex operation in APL. Not that I would know if it is any easier in any other language!
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: Transpose ragged array

Postby Bob Armstrong on Sun Jul 30, 2017 7:17 am

CoSy just has lists of lists with modulo indexing so the question in the definition of ' flip is whether to essentially flip the nub , the min of the axes involved , or the max , or some other value . Originally I simply used the length of the first item , but have since changed it to min .
User avatar
Bob Armstrong
 
Posts: 26
Joined: Wed Dec 23, 2009 8:41 pm
Location: 39.038681° -105.079070° 2500m

Re: Transpose ragged array

Postby paulmansour on Sat Aug 05, 2017 8:29 pm

Thanks Bob. Does that mean you truncate(min) or pad (max) the data? What does flipping the nub do?

I was initially as little concerned that transpose as I defined it was not self-inverse (unless the items all have the same length, a matrix). I assume none of the versions you have tried are self-inverse - that that is impossible, correct?
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: Transpose ragged array

Postby Roger|Dyalog on Wed Aug 09, 2017 2:14 pm

There is an alternative using key:

Code: Select all
      ]box on

      t3 ← {(∊⍳∘≢¨⍵) {⊂⍵}⌸ ∊⍵}

      x←(1 2 3) (4 5) (6 0 7 8 9)
      x
┌─────┬───┬─────────┐
│1 2 3│4 5│6 0 7 8 9│
└─────┴───┴─────────┘
      t3 x
┌─────┬─────┬───┬─┬─┐
│1 4 6│2 5 0│3 7│8│9│
└─────┴─────┴───┴─┴─┘

Basically, associate ⍳⍵ to each element ⍵ in an item of x, then collect together all the 0s, all the 1s, etc.
Roger|Dyalog
 
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Transpose ragged array

Postby Bob Armstrong on Wed Aug 09, 2017 8:41 pm

Here's the definition in CoSy in Forth
Code: Select all
   s" : flip " ??
: flip ( CSob -- CSob )    | Transpose object .
   dup @ if ;then      | transpose of a simple obj is itself
   dup i# 0;drop       | same for empty
   refs+> dup ( 0 i@ i# ) ['] rho 'm ,/ ['] min _./ i_ cellVecInit >aux> | ob nbdy
    i# 0 ?do dup i _nth refs+> aux@ i i! loop
    refs- aux> ;


It's a rather early definition and I might "modernize" it at some point in time , but it's not bad .

It's the only word I can think of off hand that relates two "dimensions" . One thing I learned from K is that first "dimension" does suffice .
User avatar
Bob Armstrong
 
Posts: 26
Joined: Wed Dec 23, 2009 8:41 pm
Location: 39.038681° -105.079070° 2500m


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest