This becomes easier if you want to extract rows (rather than columns) so
one option is to make the columns rows:
3 4 ([: |:&.> [ <@{."0 _ |:@]) arr

┌─────┬───────┐

│0 1 2│0 1 2 3│

│6 7 8│6 7 8 9│

│2 3 4│2 3 4 5│

│8 9 0│8 9 0 1│

│4 5 6│4 5 6 7│

│0 1 2│0 1 2 3│

│6 7 8│6 7 8 9│

│2 3 4│2 3 4 5│

│8 9 0│8 9 0 1│

│4 5 6│4 5 6 7│

└─────┴───────┘


Depending on your application it may be worthwhile thinking of changing the
format of your array so this isn't necessary.


On Sat, Sep 27, 2014 at 5:22 AM, Jon Hough <[email protected]> wrote:

> Hi Bob,
> What I want is to get certain columns of the array. I know I can use {."1
> to do this. But then I might want to get the first three columns and also
> the first 4 columns as the output.I understand that to have my verb return
> the first 3 columns and also the first 4 columns, I need to use boxing. But
> my problem seems to be in the actual loop {"1, which is the part I am
> confused about.I can't do 3 4{"(0 1) arr because the output shapes are
> different, but I am sure there is a way to do things like this.
>
> Your suggestion:
> (3&{."1 ; 4&{.) arr
> gives what I was after. However if I wanted to get an arbitrary number of
> columns, the above becomes unwieldy unless I try something like:
> columns =: 3 4 5 4 5 NB. extract the first 3,4,5,4,5 columns respectively
> ;/columns{."(0 1) arr
> I get a length error trying my above code. Hmm...
>
> > From: [email protected]
> > Date: Fri, 26 Sep 2014 10:03:52 -0700
> > To: [email protected]
> > Subject: Re: [Jprogramming] Length errors when getting multiple columns
> from array
> >
> > Hi Jon,
> >
> > I am a bit confused about what you would like as well, but without
> boxing you could do this
> >
> >    (3&{. , 4&{.)"1 arr
> > 0 1 2 0 1 2 3
> > 6 7 8 6 7 8 9
> > 2 3 4 2 3 4 5
> > 8 9 0 8 9 0 1
> > 4 5 6 4 5 6 7
> > 0 1 2 0 1 2 3
> > 6 7 8 6 7 8 9
> > 2 3 4 2 3 4 5
> > 8 9 0 8 9 0 1
> > 4 5 6 4 5 6 7
> >
> > but I am suspicious of the term 4 elements, so that what you may really
> want is this
> >
> >    (3&{."1 ; 4&{.) arr
> > ┌─────┬───────────┐
> > │0 1 2│0 1 2 3 4 5│
> > │6 7 8│6 7 8 9 0 1│
> > │2 3 4│2 3 4 5 6 7│
> > │8 9 0│8 9 0 1 2 3│
> > │4 5 6│           │
> > │0 1 2│           │
> > │6 7 8│           │
> > │2 3 4│           │
> > │8 9 0│           │
> > │4 5 6│           │
> > └─────┴───────────┘
> >
> > Cheers, bob
> >
> > On Sep 26, 2014, at 9:51 AM, Devon McCormick <[email protected]> wrote:
> >
> > > Not entirely sure what you want, but is this it?
> > >
> > >   3 4{."1&.><arr
> > > +-----+-------+
> > > |0 1 2|0 1 2 3|
> > > |6 7 8|6 7 8 9|
> > > |2 3 4|2 3 4 5|
> > > |8 9 0|8 9 0 1|
> > > |4 5 6|4 5 6 7|
> > > |0 1 2|0 1 2 3|
> > > |6 7 8|6 7 8 9|
> > > |2 3 4|2 3 4 5|
> > > |8 9 0|8 9 0 1|
> > > |4 5 6|4 5 6 7|
> > > +-----+-------+
> > >
> > >
> > > On Fri, Sep 26, 2014 at 12:44 PM, Jon Hough <[email protected]>
> wrote:
> > >
> > >> I have a 2-d array. e.g.
> > >> arr =: 10 6 $ i. 10
> > >> I want to get the first 3 columns from the array, e.g.  3{."1and get
> the
> > >> first 4 elements, e.g. 4{."1
> > >> However, combining these, as in
> > >> 3 4 {."(0 1) arrgives a length error. Of course 3{. and 4{.  will give
> > >> different shape results so should be boxed and then concatenated
> > >> perhaps(?). I am not sure how to do this, but the end result I want
> would
> > >> be something like:
> > >>
> > >> ┌─────┐
> > >>
> > >> │0 1 2│
> > >>
> > >> │6 7 8│
> > >>
> > >> │2 3 4│
> > >>
> > >> │8 9 0│
> > >>
> > >> │4 5 6│
> > >>
> > >> │0 1 2│
> > >>
> > >> │6 7 8│
> > >>
> > >> │2 3 4│
> > >>
> > >> │8 9 0│
> > >>
> > >> │4 5 6│
> > >>
> > >> └─────┘
> > >>
> > >>
> > >> concatenated with
> > >>
> > >>
> > >> ┌───────┐│0 1 2 3││6 7 8 9││2 3 4 5││8 9 0 1││4 5 6 7││0 1 2 3││6 7 8
> 9││2
> > >> 3 4 5││8 9 0 1││4 5 6 7│
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >> └───────┘
> > >>
> > >>
> > >>
> > >> ----------------------------------------------------------------------
> > >> For information about J forums see
> http://www.jsoftware.com/forums.htm
> > >
> > >
> > >
> > >
> > > --
> > > Devon McCormick, CFA
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to