ConcatRows

Revision as of 03:53, 21 February 2013 by Max (talk | contribs) (edited text and removed refs to older versions needing an external library.)


ConcatRows(a; rowIndex, colIndex , concatIndex)

Concatenates slices of array, A over index rowIndex and colIndex, resulting in a flattened array with one less dimension. The result is indexed by concatIndex, which must be an index with Size(rowIndex) * Size(colIndex) elements.

If parameter concatIndex is not specified, it creates a local index with the name .ConcatIndex, containing all combinations of elements from rowIndex and colIndex.

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 perform the inverse of ConcatRows -- in otherwords, starting with a 1-D array indexed by K, un-flatten it to a 2-D array indexed by I and J. As with ConcatRows, K must have Size(I)*Size(J) elements. The inverse is accomplished with:

Function UnconcatRows( X : Array[K] ; I,J,K : Index )
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, Rank(X,I) returns the sort rank of each element in X. The following obtains the rank among all elements in a 2-D array, A:

Var B := ConcatRows(A,I,J);
LocalAlias K := Handle(B.ConcatIndex);
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.

See Also

Comments


You are not allowed to post comments.