Indexing and Assignment

29 views
Skip to first unread message

brent.hildebrand

unread,
Dec 10, 2018, 8:04:25 PM12/10/18
to APLWin
Given the following array:

      ⎕←a←(3 3⍴⍳9)(5 3⍴10×⍳15)(4 3⍴100×⍳12)
  1 2 3     10  20  30     100  200  300
  4 5 6     40  50  60     400  500  600
  7 8 9     70  80  90     700  800  900
           100 110 120    1000 1100 1200
           130 140 150

using Pick, on can pick any single value:

      2 (2 3)⊃a
60

If I want to pick the 3rd row of the second element of a, I can use:

      3⊃⎕split 2⊃a
70 80 90

For a single element, selective assignment is easy.  Is there an equivalent for a whole row? 
      (2 (2 3)⊃a)←6000
      a
  1 2 3     10  20   30     100  200  300
  4 5 6     40  50 6000     400  500  600
  7 8 9     70  80   90     700  800  900
           100 110  120    1000 1100 1200
           130 140  150
A convoluted way would be the follow which requires some knowledge of the array and is probably very inefficient:
      ((∊(×/¨⍴¨a)⍴¨0 (0 0 0 0 0 0 1 1 1) 0)/∊a)←10000×7 8 9
      a
  1 2 3       10    20    30     100  200  300
  4 5 6       40    50  6000     400  500  600
  7 8 9    70000 80000 90000     700  800  900
             100   110   120    1000 1100 1200
             130   140   150

=============

Second question, if I want to stack these arrays on top of each other, assuming the conform in the 2nd dimension, what is the best way?  I can do it this way:

      ⍉⊃,/⍉¨a
     1     2     3
     4     5     6
     7     8     9
    10    20    30
    40    50  6000
 70000 80000 90000
   100   110   120
   130   140   150
   100   200   300
   400   500   600
   700   800   900
  1000  1100  1200

There are probably simple solutions....

brent.hildebrand

unread,
Dec 10, 2018, 9:09:01 PM12/10/18
to APLWin
I'm not seeing the reply here, but direct reply to me shows the simple solutions:

Answer to first question:  (3⌷[1]2⊃a)←1E4×7 8 9
Answer to second question:  ↑⍪/a    (DUH) 

Ajay Askoolum

unread,
Dec 11, 2018, 11:41:53 AM12/11/18
to APLWin
APL has pathways; index-origin 1 solutions
      ,[1]/a
     1    2    3
     4    5    6
     7    8    9
    10   20   30
    40   50   60
    70   80   90
   100  110  120
   130  140  150
   100  200  300
   400  500  600
   700  800  900
  1000 1100 1200
      (↑2⌷a)[3;]
70 80 90


brent.hildebrand

unread,
Dec 11, 2018, 12:14:06 PM12/11/18
to APLWin
      (↑2⌷a)[3;]
70 80 90

This fails selective assignment which was part of the problem in the first problem. The following solves that.  My first attempts were using only pick or only squad, the combo works.

      (↑2⌷a)[3;]←1E4×7 8 9
SYNTAX ERROR
      (↑2⌷a)[3;]←1E4×7 8 9
           ^
the following is a working solution: 

(3⌷[1]2⊃a)←1E4×7 8 9

In my second problem, I'm sure I tried to catenate with axis...  :-)  Just a DUH moment...

Ajay Askoolum

unread,
Dec 11, 2018, 12:51:50 PM12/11/18
to APLWin
On selective assignment:
     ⎕rl←1902 ⋄ a←(3 3⍴9?500)(5 3⍴15?500)(4 3⍴12?500)
      a
  124 384 168    252 122 158    493 407 418
  284 149 404    254 236 178    322 336 268
  357 480 344    368 427 464    258 395 482
                 373 376 152    106 284  35
                 289 381  41

Mark all odd elements as with high minus 1
      ((∊,¨×2∣a)/∊a)←¯1
      a
  124 384 168    252 122 158     ¯1  ¯1 418
  284  ¯1 404    254 236 178    322 336 268
   ¯1 480 344    368  ¯1 464    258  ¯1 482
                  ¯1 376 152    106 284  ¯1
                  ¯1  ¯1  ¯1


Ajay Askoolum

unread,
Dec 11, 2018, 1:00:57 PM12/11/18
to APLWin
Just need

Don Lagosz-Sinclair

unread,
Dec 11, 2018, 1:50:43 PM12/11/18
to APLWin
You can do the selective assignment this way:

      ((2⊃a)[3;])←99

brent.hildebrand

unread,
Dec 27, 2018, 10:20:54 AM12/27/18
to APLWin
      a←10/0
      (a⊃⍨4)←40
SYNTAX ERROR
      (a⊃⍨4)←40
          ^
      (4⊃a)←40

Apparently Commute does not work in the case of index assignment. 
Reply all
Reply to author
Forward
0 new messages