Vocabulary/StructuralOperations

From J Wiki
Jump to navigation Jump to search

Joining Arrays

Joining Arrays
What You Want To Do How To Do It
Add a new row x to the beginning of table y Use Append x , y
Add a new row x to the end of table y Use Append y , x
Add a new column x at the left of table y Use Append Items x ,. y
Add a new column x at the right of table y Use Append Items y ,. x
Join lists a,b,...,y,z as columns of a table Use a,.b,. ... y,.z
Join arrays along first axis (axis 0)

If the arrays are tables, this joins them vertically

Use Append x , y
Join arrays along axis 1

If the arrays are tables, this joins them horizontally

Use Append Items x ,. y
Join arrays along axis a Use Append with Rank x ,"(-a) y
Create an array of higher dimension with x as item 0 and y as item 1 Use Laminate x ,: y
Join arrays a,b,...y,z as successive items of an array of higher dimension Use a,b, ... y,:z
Add empty items to y, where x is a Boolean list where 0 indicates where empty items should go

1 indicates a position to be filled by an item of y. (#y) = +/x

x #^:_1 y
Add empty items to y, where x gives the number of empties to add before each item followed by the number of empties to add after the last item (#x is one more than #y) x (#^:_1~ }:@((1 #~ #) ;@:(<@({.&1);.1) _1&-))~ y

(unwieldy as this looks, it uses little memory)
or
x (#^:_1~ 1 }.@:#~ 1&j.)~ y
(runs fast)

Insert items First add empties as above; then use Amend In Place (name =: x m} name) to install the new values
Insert empty columns (items of _1-cells), x defined as above x (#^:_1"1 _1~ }:@((1 #~ #) ;@:(<@({.&1);.1) _1&-))~ y
Insert empty items into a-cells, x defined as above x (#^:_1"(1,a)~ }:@((1 #~ #) ;@:(<@({.&1);.1) _1&-))~ y

Reshaping Arrays

Reshaping Arrays
What You Want To Do How To Do It
Combine the first two axes (axes 0 and 1) ,/ y
Combine axes 1 and 2 ,./ y
Combine axes in general, where x is a Boolean list of length #$y with 0 indicating an axis to be combined with the previous axis x ((*/;.1 $) ($,) ]) y
Reshape y to shape given by x Use Reshape x ($,) y
Add a leading dimension (of length 1) Use Itemize ,: y
Reverse the order of items Use Reverse |. y
Reverse the order of k-cells Use Reverse with Rank |."k y

Selecting From Arrays

Selecting From Arrays
What You Want To Do How To Do It
Select a single item from y Use From x { y
Select item(s) x from y

x and y may have any shape. The result is an array of items of y

Use From x { y
Select column x from table y, resulting in a list x {"1 y or (<a:;x) { y
Select a set of columns x from table y, resulting in a table x {"1 y or (<a:;x) { y
Select a set of rows r and columns c from table y, resulting in a table (<r;c) { y
Select a single atom (in row r, column c) from table y (<r,c) { y

or (<r;c) { y

Select the first row of a table, resulting in a list

Actually, y can have any rank, and the result is the first item of y

Use Head {. y
Select the last row of a table, resulting in a list

Actually, y can have any rank, and the result is the last item of y

Use Tail {: y
Select the first column of a table, resulting in a list Use Head with Rank {."1 y
Select the last column of a table, resulting in a list Use Tail with Rank {:"1 y
Select the first x items of y Use Take x {. y
Select the last x items of y Use Take (-x) {. y
Select the first x columns of table y Use Take x {."1 y or (_,x) {. y
Select the last x columns of table y Use Take (-x) {."1 y or (_,-x) {. y
Select subarray of r rows and c columns from the top-left corner of table y

change signs of r and c to take from a different corner

(r,c) {. y
Select sublist with starting-point s and length l from list y Use Subarray (s,:l) ];.0 y
Select subarray with starting-corner indexes s and lengths l from array y Subarray (s,:l) ];.0 y