Mar*_*lin 3 c# arrays matrix jagged-arrays
我正在用c#编写一个程序,它将12个元素的数组与一个三元组值相关联.我想将数据存储在维度[n,m,p]的矩阵中,但每个元素实际上是一个数组.现实世界的应用是为3D笛卡尔空间中的每个点节省12个传感器的输出.
我试过这样的事情:
int[][,,] foo = new int[12][,,];
Run Code Online (Sandbox Code Playgroud)
但是,如果我是对的,那么创建一个包含12个矩阵3x3的数组,而我想要一个12个元素数组的NxMxP矩阵.
如果我尝试像这样指定矩阵尺寸:
int[][,,] foo = new int[12][N,M,P];
Run Code Online (Sandbox Code Playgroud)
我收到错误CS0178(Invalid rank specifier: expected ',' or ']')和CS1586(Array creation must have array size or array initializer).
我还在学习c#,请原谅我这个琐碎的问题,但我无法解决这个问题.我正在使用visual studio 2015.
如果你想创建组织为数组的矩阵12 实例(请注意,这是矩阵数组,而不是数组矩阵):[N, M, P]int[][,,]
int[][,,] foo = Enumerable
.Range(0, 12)
.Select(_ => new int[N, M, P])
.ToArray();
Run Code Online (Sandbox Code Playgroud)
要么
int[][,,] foo = Enumerable
.Repeat(new int[N, M, P], 12)
.ToArray();
Run Code Online (Sandbox Code Playgroud)
如果你喜欢循环
// please, notice the different declaration:
int[][,,] foo = new int[12];
for (int i = 0; i < foo.Length; ++i)
foo[i] = new int[N, M, P];
Run Code Online (Sandbox Code Playgroud)
编辑:如果你想要[N, M, P] 数组矩阵(见注释):
我正在尝试获取12个元素数组的NMP实例,可以通过n,m,p索引进行寻址
// please, notice the different declaration: matrix of arrays
int[,,][] foo = new int[N, M, P][];
for (int i = 0; i < foo.GetLength(0); ++i)
for (int j = 0; j < foo.GetLength(1); ++j)
for (int k = 0; k < foo.GetLength(2); ++k)
foo[i, j, k] = new int[12];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1599 次 |
| 最近记录: |