Transpose ragged array
7 posts
• Page 1 of 1
Transpose ragged array
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:
Is there a better way to this? Perhaps a creative use of indexing?
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
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
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!
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
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 .
-
Bob Armstrong - Posts: 26
- Joined: Wed Dec 23, 2009 8:41 pm
- Location: 39.038681° -105.079070° 2500m
Re: Transpose ragged array
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?
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
There is an alternative using key:
Basically, associate ⍳⍵ to each element ⍵ in an item of x, then collect together all the 0s, all the 1s, etc.
- 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
Here's the definition in CoSy in Forth
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 .
- 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 .
-
Bob Armstrong - Posts: 26
- Joined: Wed Dec 23, 2009 8:41 pm
- Location: 39.038681° -105.079070° 2500m
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group