문제

I have a table like the following

0 1  2  3
4 5  6  7
8 9 10 11

and I want to make the following structure.

┌──────┬──┐
│0 1  2│ 3│
│4 5  6│ 7│
│8 9 10│11│
└──────┴──┘

Could anyone please help me?

도움이 되었습니까?

해결책

And in J there is always another way!

   ]a=. i. 3 4
0 1  2  3
4 5  6  7
8 9 10 11
  ('' ;1 0 0 1) <;.1 a
┌──────┬──┐
│0 1  2│ 3│
│4 5  6│ 7│
│8 9 10│11│
└──────┴──┘

This uses the dyadic cut conjunction (;.) with the general form of x u ;. n y

y is the argument that we would like to partition, x specifies where the partitions are to be put, n is positive if we would like the frets (the partition positions) included in the result and a value of 1 means that we work from left to right, and u is the verb that we would like to apply to the partition.

One tricky point: x is ('';1 0 0 1) because we want the entire first dimension of the array (rows) after which the 1's indicate the partition start. In this case we take all the rows and make the first partition the first 3 columns, and the final 1 makes the last partition its own column.

There is much going on in this solution, and that allows it to be used in many different ways, depending on the needs of the programmer.

다른 팁

The title of your question ("slicing table into two parts and box it afterwards") suggests that the example you sketch may not reflect what you want to learn.

My impression is that you think of your resulting noun as a two-axis table boxed into two sections. The main problem with that interpretation is that boxes divide their contents very thoroughly. It takes special effort to make the numbers in your second box look like they've been trimmed from the structure in the first box. Such effort is rarely worthwhile.

If it is natural to need to take the 3 7 11 and remove it as a unit from the structure in which it occurs, there is an advantage to making it a row of the table, rather than a column. A 2-axis table is always a list of 1-axis lists. If your problem is a matter of segregating items, this orientation of the atoms makes it simpler to do.

Putting this into practice, here we deal with rows instead of columns:

  aa=: |:i.3 4
  aa
0 4  8
1 5  9
2 6 10
3 7 11

  (}: ; {:) aa
+------+------+
|0 4  8|3 7 11|
|1 5  9|      |
|2 6 10|      |
+------+------+

The program, in parentheses, can be read literally as "curtail link tail". This is the sort of program I'd expect from the title of your question.

Part of effective J programming is orienting the data (nouns) so that they are more readily manipulated by the programs (verbs).

Here is one way:

   ]a=: i. 3 4
0 1  2  3
4 5  6  7
8 9 10 11
   3 ({."1 ; }."1) a
┌──────┬──┐
│0 1  2│ 3│
│4 5  6│ 7│
│8 9 10│11│
└──────┴──┘

In other words "take the first 3 items in each row of a and Link (;) with the result of dropping the first 3 items in each row of a"

Other methods and/or structures may be more appropriate depending on the exact use case.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top