Getting the rank of a vector/matrix - Kdb+ / qStudio

Home Forums Kdb+ / qStudio Getting the rank of a vector/matrix

This topic contains 1 reply, has 2 voices, and was last updated by  matt 2 years, 5 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #111283

    ryb
    Member

    Is there something similar to monadic `ρ` (from APL) in q? It seems that you can use dyadic `#` to reshape, but is there an operator to get the shape itself?

    #111295

    matt
    Member

    Hm.. I’m not an expert but I don’t think there is. I don’t think it’s common enough to encounter in actual q code that they have a function for it. I think kdb was designed to be more minimal and a bit less general purpose than APL.

    In fact, before v3.4, reshaping was only limited to 2 dimensions http://code.kx.com/wiki/Reference/NumberSign. Also, you have to remember that kdb’s vectors are very flexible and are able to have items of different lengths and types.

    The closest thing you could probably do is to use the count function. You could use the each adverb to easily get the lengths at the different dimensions.
    `
    q) l: ((1 2; 3); (1; 2 3); (1 2 3))
    q) l
    (1 2;3)
    (1;2 3)
    1 2 3
    q) count l / # of items (1st dimension)
    3
    q) count each l / # of items per item (2nd dimension), notice that it may vary
    2 2 3
    q) (count each) each l / # of items per item per item (3rd dimension), non-lists have a count of 1
    2 1
    1 2
    1 1 1
    `
    Notice how you can derive all 3 dimensions just from the last one (based on the number of rows or columns returned).

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.