Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compose matrix out of several subarrays in J

Tags:

arrays

j

I want to compose a 12x12 matrix named F out of 4 given smaller submatrices which should be located at different positions:

  • array A of shape 3x6 should be from (0;0) to (2;5)
  • array B of shape 4x9 should be from (3;3) to (6;11)
  • array C of shape 3x3 should be from (7;0) to (9;2)
  • array D of shape 2x3 should be from (10;6) to (11;8)

All other atoms are zeros. I started setting up F =: 12 12 $ 0 but failed trying the amend verb. What would be best practice for this?

My subarrays are: A =: 3 6 $ _1 1 0 0 0 0 0 0 _1 0 0 1 0 0 0.99 0 _1 0 B =: 4 9 $ 1 0 0 1 0 0 _1 0 0 0 1 0 0 0 0 0 _1 0 0 1 0 0 _1 0 0 0 0 1 0 1 1 0 1 1 0 1 C =: 3 3 $ 1 0 0 0 1 0 0 0 1 D =: 2 3 $ 1 0 0 0 0 1

like image 973
smartmic Avatar asked Mar 09 '23 08:03

smartmic


1 Answers

Make a list of coordinates from the shape of each array.

 c_D =: {@(;&i.)/ $ D
┌───┬───┬───┐
│0 0│0 1│0 2│
├───┼───┼───┤
│1 0│1 1│1 2│
└───┴───┴───┘

add the offset to the above coordinates

c_D =: (<10 6) + &.> c_D

and now use amend:

D c_D } F

You can form a gerund to streamline this process, something along the lines of:

g =: 3 : '({.y) +&.> {@(;&i.)/$ >{:y'
m =: ((>@{:@[)`(g@[)`])

((0 0);A) m} F
((3 3);B) m} F
etc.
like image 145
Eelvex Avatar answered Mar 21 '23 08:03

Eelvex