smartmic
smartmic

Reputation: 700

Compose matrix out of several subarrays in J

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

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

Upvotes: 4

Views: 108

Answers (2)

bob
bob

Reputation: 4302

A slightly different approach that could work if the components have consistent shapes involves padding out the component arrays and then assembling the 12X12 array.

  12{."1. A     NB. Pad 0's 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's 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

Then assemble the final array

   (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

Upvotes: 3

Eelvex
Eelvex

Reputation: 9143

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.

Upvotes: 4

Related Questions