Partitionning a vector question

For users of dfns, both novice and expert
Post Reply
User avatar
PGilbert
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Partitionning a vector question

Post by PGilbert »

Hello everyone, I am trying to partition an even vector as follow:

      {↓(((0.5×⍴⍵),2)⍴⍵)}⍳10
1 2 3 4 5 6 7 8 9 10


is someone has a more 'elegant' solution than going through a matrix for doing that.

Thanks in advance.
Roger|Dyalog
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Partitionning a vector question

Post by Roger|Dyalog »

      x⊂⍨1 0⍴⍨≢x

I don't know whether you'd consider it more "elegant". It IS shorter. It's not quite as fast as ↓ of a 2-column matrix because ↓ can exploit the regularity that is not practically detectable by ⊂.
User avatar
PGilbert
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Partitionning a vector question

Post by PGilbert »

Hello Roger, I am not obtaining the expected result for ⍳10:
      {⍵⊂⍨1 0⍴⍨≢⍵}⍳10
1 3 5 7 9
Roger|Dyalog
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Partitionning a vector question

Post by Roger|Dyalog »

The expression requires ⎕ml≤2.
User avatar
PGilbert
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Partitionning a vector question

Post by PGilbert »

Thanks its working with ⎕ml←2 (I was ⎕ml←3)
Veli-Matti
Posts: 94
Joined: Sat Nov 28, 2009 3:12 pm

Re: Partitionning a vector question

Post by Veli-Matti »

Hi,
Roger's solution may be made slightly shorter by
x⊂⍨2|⍳≢x
but this means that (⎕ML ⎕IO)←0 1
(and I'm quite sure that Roger won't use 1 for index origin..)

Tricks aside, you may use either Dyalog partition
{((≢⍵)⍴1 0)⊂⍵⊣⎕ML←2}
or APL2 style
{((≢⍵)⍴2 1)⊂⍵⊣⎕ML←3}

-wm
User avatar
PGilbert
Posts: 440
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Partitionning a vector question

Post by PGilbert »

Thanks Veli-Matti and Roger, I learned a little bit more about partition today and the interaction of ⎕ML. Since we are using ⎕ML←3, the expression {((⍴⍵)⍴2 1)⊂⍵} is what I was looking for.
Post Reply