Access same row/column in each array in nested arrays

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

Access same row/column in each array in nested arrays

Postby KamakaniKing on Fri Apr 02, 2021 3:43 am

Is there a way to multiply or add a scalar to the same row or column in each array of a nested array without a loop? For example I would like to multiply all the values in the 2nd column (or row) of each array by 2 for the sample nested array below.

      ┌→─────────────────────────────────┐
│ ┌→───────┐ ┌→───────┐ ┌→───────┐ │
│ ↓ 1 2 3│ ↓ 4 8 10│ ↓ 9 46 30│ │
│ │ 4 5 6│ │ 3 16 10│ │65 13 67│ │
│ │ 7 8 9│ │ 2 16 19│ │55 50 3│ │
│ │10 11 12│ │ 4 9 4│ └~───────┘ │
│ └~───────┘ │13 6 10│ │
│ └~───────┘ │
└∊─────────────────────────────────┘
KamakaniKing
 
Posts: 9
Joined: Thu Mar 25, 2021 8:06 pm

Re: Access same row/column in each array in nested arrays

Postby Phil Last on Fri Apr 02, 2021 9:38 am

I gave up copying your numbers and just did
      zz←(4 3)(5 3)(3 3)⍴¨⊂⍳15
so:
      zz
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
6 7 8 6 7 8 6 7 8
9 10 11 9 10 11
12 13 14
⍝ twice the 2th row of each matrix
2×@2¨zz
0 1 2 0 1 2 0 1 2
3 4 5 3 4 5 3 4 5
12 14 16 12 14 16 12 14 16
9 10 11 9 10 11
12 13 14
⍝ twice the 2th item of each row of each matrix
2×@2⍤1¨zz
0 1 4 0 1 4 0 1 4
3 4 10 3 4 10 3 4 10
6 7 16 6 7 16 6 7 16
9 10 22 9 10 22
12 13 28
User avatar
Phil Last
 
Posts: 572
Joined: Thu Jun 18, 2009 6:29 pm

Re: Access same row/column in each array in nested arrays

Postby KamakaniKing on Fri Apr 02, 2021 2:26 pm

Phil - Thx! Perfect. Sorry for the lousy sample array....
KamakaniKing
 
Posts: 9
Joined: Thu Mar 25, 2021 8:06 pm

Re: Access same row/column in each array in nested arrays

Postby KamakaniKing on Fri Apr 02, 2021 3:48 pm

Phil - Assuming the nested arrays (zz) are all (n by 2) matrices, is there a way to multiply each by a 2x2 rotation matrix (r): zz +.× r
KamakaniKing
 
Posts: 9
Joined: Thu Mar 25, 2021 8:06 pm

Re: Access same row/column in each array in nested arrays

Postby Phil Last on Sat Apr 03, 2021 10:29 am

You can just put the +.× under the each ¨ operator.

If r is equally a vector of different 2 by 2 matrices then
      zz +.× ¨ r
If there is just a single matrix then enclose it
      zz +.× ¨ ⊂r
There might be a more efficient way by restructuring the data using more dimensions but the above should work.
User avatar
Phil Last
 
Posts: 572
Joined: Thu Jun 18, 2009 6:29 pm

Re: Access same row/column in each array in nested arrays

Postby KamakaniKing on Sat Apr 03, 2021 5:34 pm

Phil - Thx! This works great!
KamakaniKing
 
Posts: 9
Joined: Thu Mar 25, 2021 8:06 pm


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest