我有两个数组。一个包含“真实”值,另一个包含“虚构”值。这两个数组需要组合成一个复数数组。我尝试了以下几种方法:
Complex[] complexArray = new Complex[16384];
for (int i = 0; i <16384; i++)
(
complexArray[i].Real = realArray[i];
complexArray[i].Imaginary = imaginaryArray[i];
}它不起作用。它给出了错误:属性或索引器'System.Numerics.Complex.Real‘不能被赋值--它是只读的。我知道复数是不可变的,但是如何创建这样的数组呢?
更重要的是,一旦我有了这个数组,我就想要在其中移动值。
发布于 2015-01-28 06:14:13
只需使用Complex的构造函数
Complex[] complexArray = new Complex[16384];
for (int i = 0; i < complexArray.Length; i++)
(
complexArray[i] = new Complex(realArray[i], imaginaryArray[i]);
}然后,您可以选择使用LINQ来减少代码量(稍微降低性能成本):
var complexArray = realArray.Zip(imaginaryArray, (a, b) => new Complex(a, b)).ToArray();要移动数组中的值,请执行与值为int或double相同的操作
int i = 5;
int j = 7;
// Swap positions i and j
var temp = complex[i];
complex[i] = complex[j];
complex[j] = temp;https://stackoverflow.com/questions/28181018
复制相似问题