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; rowIndex, colIndex '', concatIndex'') =
+
=== ConcatRows(a, i, j'', k'') ===
  
"Flattens" array ''a'' by replacing indexes ''rowIndex'' and ''colIndex'' with a new index ''concatIndex'' that includes all combinations of values from ''rowIndex'' and ''colIndex''. If you don't specify ''concatIndex'', it generates a local index with the name .ConcatIndex, containing all combinations of elements from ''rowIndex'' and ''colIndex''. If you do specify ''concatIndex'', it must have length equal to''[[Size]](rowIndex) * [[Size]](colIndex) elements.
+
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) || &rarr; ||  
+
| [[ConcatRows]](A, I, J) || &rarr; ||  
 
{| 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) || &rarr; ||  
+
| [[ConcatRows]](A, J, I) || &rarr; ||  
 
{| 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 perform the inverse of [[ConcatRows]] -- in otherwords, starting with a 1-D array indexed by <code>K</code>, un-flatten it to a 2-D 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.  The inverse is accomplished with:
+
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]
  
With this inverse function, you can extend a variety of 1-D transformation functions to 2-D equivalents.  For example, <code>[[Rank]](X,I)</code> returns the sort rank of each element in <code>X</code>.  The following obtains the rank among all elements in a 2-D array, <code>A</code>:
+
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 1-D transformation functions that can be usefully extended to 2-D equivalents in this fashion include: [[Sort]], [[Cumulate]], [[Uncumulate]], [[CumProduct]],  [[Dispatch]], [[GetFract]], [[Frequency]], and perhaps others.  The technique could be applied to the [[Integrate]], [[Cdf]], [[Pdf]], [[LinearInterp]] and [[CubicInterp]] functions, but in these cases, this would not be the same as what you would normally think of as the 2-D generalization of the functions, so use with care in those cases.
+
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:=
J
1 2 3 4
I 1 'a' 'b' 'c' 'd'
2 'e' 'f' 'g' 'h'
ConcatRows(A, I, J)
.ConcatIndex: 1 2 3 4 5 6 7 8
'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h'
ConcatRows(A, J, I)
.ConcatIndex: 1 2 3 4 5 6 7 8
'a' 'e' 'b' 'f' 'c' 'g' 'd' 'h'

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.

See Also

Comments


You are not allowed to post comments.