Difference between revisions of "ConcatRows"
m (Clean up) |
|||
Line 2: | Line 2: | ||
[[Category:Doc Status C]] <!-- For Lumina use, do not change --> | [[Category:Doc Status C]] <!-- For Lumina use, do not change --> | ||
− | = ConcatRows(a | + | === ConcatRows(a, i, j'', k'') === |
− | + | Flattens array «a» by replacing the two indexes «i» and «j» by a single new index «k» that includes all combinations of values from «i» and «j». If you don't specify «i», it generates a local index with the name .ConcatIndex, containing all those combinations. If you do specify «i», it must have length equal to [[Size]](«i») * [[Size]]( «j»)''.'' | |
− | = Examples = | + | === Examples === |
Let | Let | ||
Line 25: | Line 25: | ||
{|border="0" | {|border="0" | ||
− | | [[ConcatRows]](A,I,J) || → || | + | | [[ConcatRows]](A, I, J) || → || |
{| border="1" | {| border="1" | ||
! .ConcatIndex: !! 1 !! 2 !! 3 !! 4 !! 5 !! 6 !! 7 !! 8 | ! .ConcatIndex: !! 1 !! 2 !! 3 !! 4 !! 5 !! 6 !! 7 !! 8 | ||
Line 34: | Line 34: | ||
{|border="0" | {|border="0" | ||
− | | [[ConcatRows]](A,J,I) || → || | + | | [[ConcatRows]](A, J, I) || → || |
{| border="1" | {| border="1" | ||
! .ConcatIndex: !! 1 !! 2 !! 3 !! 4 !! 5 !! 6 !! 7 !! 8 | ! .ConcatIndex: !! 1 !! 2 !! 3 !! 4 !! 5 !! 6 !! 7 !! 8 | ||
Line 43: | Line 43: | ||
To flatten three dimensions, I1, I2 and I3, use: | To flatten three dimensions, I1, I2 and I3, use: | ||
− | :[[Var..Do|Var]] tmp := [[ConcatRows]](A,I1,I2) Do [[ConcatRows]](tmp,tmp.ConcatIndex,I3) | + | :[[Var..Do|Var]] tmp := [[ConcatRows]](A, I1, I2) Do [[ConcatRows]](tmp, tmp. ConcatIndex,I3) |
− | = Inverse of ConcatRows = | + | === Inverse of ConcatRows === |
− | It is often useful to | + | It is often useful to do the inverse of [[ConcatRows]] -- that is, unflatten a 1D array indexed by <code>K</code> to produce a 2D array indexed by <code>I</code> and <code>J</code>. As with [[ConcatRows]], <code>K</code> must have <code>[[Size]](I)*[[Size]](J)</code> elements. You can do this by defining this function: |
− | Function UnconcatRows( X : Array[K] ; I,J,K : Index ) | + | Function UnconcatRows(X: Array[K]; I, J, K: Index) |
− | Definition: X[K=(@I-1)*[[Size]](J) + @J] | + | Definition: X[K=(@I - 1)*[[Size]](J) + @J] |
− | + | You can use this function to extend a variety of 1D transformation functions to their 2D equivalents. For example, <code>[[Rank]](X, I)</code> returns the sort rank of each element in <code>X</code>. Given a 2D array,, apply ConcatRows() to flatten it, then apply the 1D transformation, <code>[[Rank]](),</code> to the 1D result, and finally use UnConcatRows() to get back the 2D array of ranks: | |
− | [[Var]] B := [[ConcatRows]](A,I,J); | + | [[Var]] B := [[ConcatRows]](A, I, J); |
[[LocalAlias]] K := [[Handle]](B.ConcatIndex); | [[LocalAlias]] K := [[Handle]](B.ConcatIndex); | ||
− | UnconcatRows( [[Rank]](B,K), I,J,K ) | + | UnconcatRows([[Rank]](B, K), I, J, K) |
− | Some other | + | Some other 1D transformation functions that can be usefully extended to 2D equivalents in this fashion are: [[Sort]], [[Cumulate]], [[Uncumulate]], [[CumProduct]], [[Dispatch]], [[GetFract]], [[Frequency]]. The technique could be applied to [[Integrate]], [[Cdf]], [[Pdf]], [[LinearInterp]] and [[CubicInterp]] functions, but the results might not be quite the same as what you would normally think of as the 2D generalization of the functions, so use with care in those cases. |
− | = See Also = | + | === See Also === |
* [[MdArrayToTable]] | * [[MdArrayToTable]] | ||
* [[Concat]] | * [[Concat]] |
Revision as of 18:37, 20 November 2015
ConcatRows(a, i, j, k)
Flattens array «a» by replacing the two indexes «i» and «j» by a single new index «k» that includes all combinations of values from «i» and «j». If you don't specify «i», it generates a local index with the name .ConcatIndex, containing all those combinations. If you do specify «i», it must have length equal to Size(«i») * Size( «j»).
Examples
Let
A:= |
|
---|
ConcatRows(A, I, J) | → |
|
ConcatRows(A, J, I) | → |
|
To flatten three dimensions, I1, I2 and I3, use:
- Var tmp := ConcatRows(A, I1, I2) Do ConcatRows(tmp, tmp. ConcatIndex,I3)
Inverse of ConcatRows
It is often useful to do the inverse of ConcatRows -- that is, unflatten a 1D array indexed by K
to produce a 2D array indexed by I
and J
. As with ConcatRows, K
must have Size(I)*Size(J)
elements. You can do this by defining this function:
Function UnconcatRows(X: Array[K]; I, J, K: Index) Definition: X[K=(@I - 1)*Size(J) + @J]
You can use this function to extend a variety of 1D transformation functions to their 2D equivalents. For example, Rank(X, I)
returns the sort rank of each element in X
. Given a 2D array,, apply ConcatRows() to flatten it, then apply the 1D transformation, Rank(),
to the 1D result, and finally use UnConcatRows() to get back the 2D array of ranks:
Var B := ConcatRows(A, I, J); LocalAlias K := Handle(B.ConcatIndex); UnconcatRows(Rank(B, K), I, J, K)
Some other 1D transformation functions that can be usefully extended to 2D equivalents in this fashion are: Sort, Cumulate, Uncumulate, CumProduct, Dispatch, GetFract, Frequency. The technique could be applied to Integrate, Cdf, Pdf, LinearInterp and CubicInterp functions, but the results might not be quite the same as what you would normally think of as the 2D generalization of the functions, so use with care in those cases.
Enable comment auto-refresher