Partitionning a vector question

For users of dfns, both novice and expert

Partitionning a vector question

Postby PGilbert on Tue Dec 15, 2015 5:11 pm

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.
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Partitionning a vector question

Postby Roger|Dyalog on Tue Dec 15, 2015 6:44 pm

      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 ⊂.
Roger|Dyalog
 
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Partitionning a vector question

Postby PGilbert on Tue Dec 15, 2015 7:20 pm

Hello Roger, I am not obtaining the expected result for ⍳10:
      {⍵⊂⍨1 0⍴⍨≢⍵}⍳10
1 3 5 7 9
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Partitionning a vector question

Postby Roger|Dyalog on Tue Dec 15, 2015 7:26 pm

The expression requires ⎕ml≤2.
Roger|Dyalog
 
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Partitionning a vector question

Postby PGilbert on Tue Dec 15, 2015 7:30 pm

Thanks its working with ⎕ml←2 (I was ⎕ml←3)
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: Partitionning a vector question

Postby Veli-Matti on Tue Dec 15, 2015 7:32 pm

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
Veli-Matti
 
Posts: 93
Joined: Sat Nov 28, 2009 3:12 pm

Re: Partitionning a vector question

Postby PGilbert on Tue Dec 15, 2015 8:03 pm

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.
User avatar
PGilbert
 
Posts: 436
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada


Return to Functional Programming

Who is online

Users browsing this forum: No registered users and 1 guest