Reputation: 577
I have two arrays. One contains "real" values and the other contains "imaginary" values. These two arrays need to be combined into an array of complex numbers. I tried the following:
Complex[] complexArray = new Complex[16384];
for (int i = 0; i <16384; i++)
(
complexArray[i].Real = realArray[i];
complexArray[i].Imaginary = imaginaryArray[i];
}
It doesn't work. It gives the error: Property or indexer 'System.Numerics.Complex.Real' cannot be assigned to -- it is read only I understand that complex numbers are immutable but how then does one create such an array?
Even more so, once I have this array, I want to move values in it.
Upvotes: 2
Views: 3143
Reputation: 79441
Just use the constructor for Complex
:
Complex[] complexArray = new Complex[16384];
for (int i = 0; i < complexArray.Length; i++)
(
complexArray[i] = new Complex(realArray[i], imaginaryArray[i]);
}
Optionally, you can then reduce the amount of code (slight performance cost) by using LINQ:
var complexArray = realArray.Zip(imaginaryArray, (a, b) => new Complex(a, b)).ToArray();
To move values in your array, do the same thing as if the values were int
or double
:
int i = 5;
int j = 7;
// Swap positions i and j
var temp = complex[i];
complex[i] = complex[j];
complex[j] = temp;
Upvotes: 6