Compose a matrix of several subarrays in J

I want to compose a 12x12 matrix with the name F of 4, given the smaller sub-matrices, which should be located in different positions:

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

All other atoms are zeros. I started to tune F =: 12 12 $ 0, but could not execute change the verb. What would be best for this?

My subarrays: 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

+4
source share
2 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 offset to the above coordinates

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

:

D c_D } F

gerund , - :

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

((0 0);A) m} F
((3 3);B) m} F
etc.
+4

, , , 12X12.

  12{."1. A     NB. Pad 0 to the right
_1 1    0 0  0 0 0 0 0 0 0 0
 0 0   _1 0  0 1 0 0 0 0 0 0
 0 0 0.99 0 _1 0 0 0 0 0 0 0
   _12{."1 B    NB. Pad 0 to the left
0 0 0 1 0 0 1  0 0 _1  0 0
0 0 0 0 1 0 0  0 0  0 _1 0
0 0 0 0 1 0 0 _1 0  0  0 0
0 0 0 1 0 1 1  0 1  1  0 1
   12{."1. C
1 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0
   _12{."1 [ 6 {."1 D   NB. extra {. required to pad both ends
0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0

   (12{."1. A) , (_12 {."1 B),(12 {."1 C),_12{."1[ 6 {."1 D
_1 1    0 0  0 0 0  0 0  0  0 0
 0 0   _1 0  0 1 0  0 0  0  0 0
 0 0 0.99 0 _1 0 0  0 0  0  0 0
 0 0    0 1  0 0 1  0 0 _1  0 0
 0 0    0 0  1 0 0  0 0  0 _1 0
 0 0    0 0  1 0 0 _1 0  0  0 0
 0 0    0 1  0 1 1  0 1  1  0 1
 1 0    0 0  0 0 0  0 0  0  0 0
 0 1    0 0  0 0 0  0 0  0  0 0
 0 0    1 0  0 0 0  0 0  0  0 0
 0 0    0 0  0 0 1  0 0  0  0 0
 0 0    0 0  0 0 0  0 1  0  0 0
+3

Source: https://habr.com/ru/post/1683590/


All Articles