[英]How to make a ZeroMatrix in 3D array C# (function)
我有一个函数可以在2D数组中制作zeromatrix,如下所示:
public double[][] ZeroMatrix(int rows, int cols)
{
int i, j;
double[][] m = new double[rows][];
for (i = 0; i < rows; i++)
{
m[i] = new double[cols];
for (j = 0; j < cols; j++)
m[i][j] = 0;
}
return m;
}
如何制作可处理3D阵列的矩阵? 也许是这样的:
public double[][][] ZeroMatrix(int rows, int cols, int etc)
{
...
...
return m;
}
我尝试了一些代码,但总是出错。
我认为最好使用多维数组来解决此问题:
public double[,,] ZeroMatrix(int rows, int cols, int etc)
{
double[,,] m = new double[rows, cols, etc];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
for(int k = 0 ; k < etc ; k++)
m[i , j , k] = 0;
return m;
}
有关C#中多维数组的更多信息,请阅读此内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.