C#实现任意维度数组切片均值的递归计算方案问询
通用递归实现任意维度数组的切片均值计算
要实现任意维度数组的切片均值计算(输入N维数组输出N-1维数组,每个元素对应原数组第一个维度切片的均值),核心思路是利用递归+Array类的通用API处理任意维度,替代针对固定维度的冗余代码:
- 基准情况:当输入是一维数组时,直接返回包含该数组均值的一维数组。
- 递归降维:对于N>1维的数组,先创建一个N-1维的结果数组(维度长度对应原数组从第1维到最后一维),然后递归遍历结果数组的所有索引组合,对每个索引组合,提取原数组第一个维度上的所有元素计算均值,赋值给结果数组的对应位置。
完整实现代码
using System; public class ArraySliceMeanCalculator { public static Array ComputeSliceMean(Array inputArray) { if (inputArray == null) throw new ArgumentNullException(nameof(inputArray)); // 基准情况:一维数组,返回包含均值的一维数组 if (inputArray.Rank == 1) { int length = inputArray.Length; if (length == 0) throw new ArgumentException("Input array cannot be empty.", nameof(inputArray)); double sum = 0; for (int i = 0; i < length; i++) { sum += Convert.ToDouble(inputArray.GetValue(i)); } return new double[] { sum / length }; } // 递归情况:创建N-1维结果数组 int newRank = inputArray.Rank - 1; int[] newDimensions = new int[newRank]; for (int i = 0; i < newRank; i++) { newDimensions[i] = inputArray.GetLength(i + 1); } Array resultArray = Array.CreateInstance(typeof(double), newDimensions); // 遍历结果数组的所有索引组合,计算对应切片均值 TraverseAndComputeMean(resultArray, 0, new int[newRank], inputArray); return resultArray; } private static void TraverseAndComputeMean(Array resultArray, int currentDimension, int[] currentIndices, Array inputArray) { // 遍历到最后一个维度,计算当前索引对应的切片均值 if (currentDimension == resultArray.Rank) { int firstDimLength = inputArray.GetLength(0); if (firstDimLength == 0) throw new ArgumentException("First dimension of input array cannot be empty.", nameof(inputArray)); double sum = 0; for (int i = 0; i < firstDimLength; i++) { // 构造原数组的索引:第0维遍历所有元素,后续维度匹配当前索引 int[] inputIndices = new int[inputArray.Rank]; inputIndices[0] = i; Array.Copy(currentIndices, 0, inputIndices, 1, currentIndices.Length); sum += Convert.ToDouble(inputArray.GetValue(inputIndices)); } double mean = sum / firstDimLength; resultArray.SetValue(mean, currentIndices); return; } // 递归遍历当前维度的所有索引 int dimLength = resultArray.GetLength(currentDimension); for (int i = 0; i < dimLength; i++) { currentIndices[currentDimension] = i; TraverseAndComputeMean(resultArray, currentDimension + 1, currentIndices, inputArray); } } }
关键细节说明
- 通用数组处理:使用
Array类而非强类型多维数组(如int[,]),兼容任意维度输入,通过Rank获取维度数、GetLength获取各维度长度、GetValue/SetValue操作元素。 - 递归遍历索引:通过
TraverseAndComputeMean递归遍历结果数组的所有索引组合,彻底避免了针对不同维度编写嵌套循环的冗余代码。 - 数值类型兼容:通过
Convert.ToDouble统一转换输入元素类型,最终返回double类型的结果数组,支持int、float、double等常见数值类型的输入。
使用示例
// 测试二维数组 int[,] twoDArray = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } }; Array twoDResult = ArraySliceMeanCalculator.ComputeSliceMean(twoDArray); double[] oneDMean = (double[])twoDResult; // 输出:[2.5, 3.5, 4.5] // 测试四维数组 int[,,,] fourDArray = new int[2, 2, 2, 2] { { { {1,2}, {3,4} }, { {5,6}, {7,8} } }, { { {9,10}, {11,12} }, { {13,14}, {15,16} } } }; Array fourDResult = ArraySliceMeanCalculator.ComputeSliceMean(fourDArray); double[,,] threeDMean = (double[,,])fourDResult; // 示例元素:threeDMean[0,0,0] = (1+9)/2 = 5.0
内容的提问来源于stack exchange,提问作者user129994
相关产品推荐
相关产品推荐

