Question

Using ILNumerics, I am trying to take the first n number of columns of an ILArray<> in the most efficient way possible.

using(ILScope.Enter(inframe)
{
    ILArray<complex> frame = ILMath.check(inframe);
    int[] dims = frame.Size.ToIntArray(); //frame is a 2d ILArray
    frame.SetRange(complex.Zero, dims[0] -1 , (dims[1] * 2 - 1)); //doubles the size of the array with zeros.
    //TODO- various computations.
    frame.SetRange(null, dims[0], dims[1] - 1); //EXCEPTION: why doesn't this work?
}

In this example I am trying to take only the first half of the frame, but I am unable to size it back to the original dimensions. I have tried various permutations based on http://ilnumerics.net/ArrayAlter.html but have been unsuccessful.

Was it helpful?

Solution

The documentation for shrinking of ILNumerics arrays says:

The range definition must address the full dimension - for all dimensions except the one, which is to be removed.

You want to remove the last half from the 2nd dimension. So you must define full ranges for all other dimensions involved. Here, since frame is a matrix, there are only 2 dimensions. Hence, the first must get fully addressed.

It should work easier by using the C# indexer. The following example assumes your code in a class derived from ILMath. Otherwise, add ILMath. before all the full, r, and end functions / properties:

A[full, r(end / 2, end)] = null;

Watch out for ‘off by one’ errors and addressing with ‘end’. You may want to use end / 2 + 1 instead ?

Since you want the most efficient way, performance seems to be important to you. In this case, you should try to prevent from expanding and shrinking arrays! It is better to work with two arrays of different sizes: a large one and the original one. Copy the data accordingly. Expanding and shrinking does copy the data anyway, so this is not a disadvantage. Furthermore, frame.Size.ToIntArray() is not needed here. Simply use frame.S[0]and frame.S[1] for the length of the dimensions 0 and 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top