KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ high dimensional matrix index

  • high dimensional matrix index

    Posted by lestat-jin on February 6, 2025 at 12:58 pm

    apologies if this is obvious for  Q professionals, I’m struggling to understand why running below in console returns 1 2 3 4 5 6,  not 5 6  ?  even ChatGPT says it returns 5 6…

    mm:((1 2;3 4;5 6);(10 20;30 40;50 60))

    mm 0 2

    lestat-jin replied 1 week, 5 days ago 3 Members · 4 Replies
  • 4 Replies
  • unterrainer_ale

    Member
    February 6, 2025 at 1:06 pm

    When indexing at depth you need the square brackets or index-at for nested indexing dot-apply

    mm:((1 2;3 4;5 6);(10 20;30 40;50 60))
    mm 0 2
    1 2 3 4 5 6
    
    0N!mm 0 2
    ((1 2;3 4;5 6);(`long$();`long$();`long$()))
    1 2 3 4 5 6
    
    mm[0;2]
    5 6
    mm . (0;2)
    5 6
    mm . 0 2
    
    5 6   

    If you really wanted you could use simple apply and the iterator over to achieve what you are looking for

    mm@/0 2
    5 6

    If you use 0N! you can see that when using mm 0 2 you’re basically indexing at the first level, first for index 0 which returns the first top level, then at index 2 which doesn’t exist hence you get a list of empty long lists
    3. Lists – Q for Mortals
    Apply (At), Index (At), Trap (At) | Reference | kdb+ and q documentation – kdb+ and q documentation

    • lestat-jin

      Member
      February 9, 2025 at 5:02 pm

      Thanks!  good point to know!

  • mwoods

    Administrator
    February 6, 2025 at 1:11 pm

    To return 5 6 you will need to index at depth by passing a semicolon

    mm[0;2]
    5 6

    Indexing by passing a list of integers returns all indexes so in this case:

    mm 0 // returns the element at position 0
    1 2
    3 4
    5 6

    mm 2 // returns the element at position 2 - which doesn't exist

    mm 0 2 // get both elements at positions 0 and 2 - because none at 2 we just get the 0 elements back 
    1 2 3 4 5 6
      

    This concept covered in the free academy course in the module on List Amendment and eliding at depth.

    https://learninghub.kx.com/courses/kdb-developer-level-2/lessons/lists/topic/list-indexing-access/

    • lestat-jin

      Member
      February 9, 2025 at 5:03 pm

      Thanks! maybe time to refresh my memory of the course did some time ago…

Log in to reply.