index function

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

index function

Postby BenoitM on Sun Apr 11, 2021 2:45 am

      +a←5 4 ⍴ ⍳20
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
a[2;]
5 6 7 8
a[;3]
3 7 11 15 19
2 ⌷ a
5 6 7 8

but how do you extract the 3rd column using the index function?
BenoitM
 
Posts: 10
Joined: Tue Jan 12, 2021 3:04 pm

Re: index function

Postby petermsiegel on Sun Apr 11, 2021 3:35 am

Extracting the 3rd column of a matrix (⎕IO=1)
      3⌷[2]a    ⍝ Index fn w/ axis op (special syntax)
3 7 11 15 19
3⌷⍤1⊣a ⍝ Index fn w/ rank op
3 7 11 15 19
a[;3] ⍝ Bracket Indexing
3 7 11 15 19

And...
      COL←(⌷⍤1)
3 COL a
3 7 11 15 19
petermsiegel
 
Posts: 140
Joined: Thu Nov 11, 2010 11:04 pm

Re: index function

Postby Adam|Dyalog on Sun Apr 11, 2021 7:06 am

In addition to what petermsiegel wrote, it is worth noting that if you are going to extract columns repeatedly, it may be worthwhile to transpose your matrix once.
      a←?1000 1000⍴0
aT←⍉a
]runtime -c 3⌷[2]a 3⌷⍤1⊢a a[;3] 3⌷aT

3⌷[2]a → 3.1E¯6 | 0% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
3⌷⍤1⊢a → 2.8E¯6 | -8% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
a[;3] → 2.6E¯6 | -16% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
3⌷aT → 2.9E¯7 | -91% ⎕⎕⎕⎕
User avatar
Adam|Dyalog
 
Posts: 134
Joined: Thu Jun 25, 2015 1:13 pm

Re: index function

Postby mwr0707 on Tue Nov 09, 2021 4:21 pm

I was scratching my head over this until I realized that by employing the intermediate variable aT you meant "extract columns repeatedly from the SAME array".
      a←?1000 1000⍴0
]runtime -c 3⌷[2]a 3⌷⍤1⊢a a[;3] 3⌷⍉a

3⌷[2]a → 2.0E¯6 | 0%
3⌷⍤1⊢a → 2.0E¯6 | 0%
a[;3] → 1.5E¯6 | -25%
3⌷⍉a → 2.3E¯3 | +118200%

:-)
mwr0707
 
Posts: 16
Joined: Wed Oct 28, 2015 9:49 am


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest