Based on these two posts (here and here), I understand that it may be possible to write my own extension method for grabbing a 2D slice from a 3D array. I'm working with 3D arrays that represent spatial data in the following way: [layer, row, col]
. I'd like to slice over the layer index such that I get a 2D array of all row, col
values for the selected layer. However, I haven't been able to work out a way to return a 2D array from the extension method. Here is what I've tried so far, but it errors as noted by the comment // Error: ...
:
double[,,] hds // 3D array of values
double[,] lay_hd; // 2D slice to be filled by extension method
for(int k = 0; k < nlay; k++)
{
lay_hd = gwhds.SliceLayer(k); // Error: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<double>' to 'double[*,*]'
// write 2D array to text file...
}
public static IEnumerable<T> SliceLayer<T>(this T[,,] array, int lay)
{
for (var i = array.GetLowerBound(1); i <= array.GetUpperBound(1); i++)
{
for (var j = array.GetLowerBound(2); j <= array.GetUpperBound(2); j++)
{
yield return array[lay, i, j];
}
}
}
Is it possible to return a 2D array from an extension method?