Converting a multidimensional array to 2 single dimensional arrays

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

I have a multidimensional array defined as
private double[,] myArray = new double[ 2, 1024];
The first column of the array contains X values, the other contains Y values
I have a charting function defined as
Add(Array XValues, Array YValues)

How do I call the Add function, passing my array columns please.

thanks
 
Your data is fundamentally in the wrong storage format to be amenable to
that Add() method.

You can do two things here. One, do like the other person said and change
from a 2-d array, double[,], to an array-of-arrays double[][]. The other is
to create temporary arrays containing the columns just for the purpose of
the Add() call:

double XValues[] = new double[1024];
double YValues [] = new double[1024];
for(int i =0; i < 1024, i++) {
XValues = myArray[0,i];
YValues = myArray[1,i];
}
Add(XValues, YValues);

If this is an isolated situation and it really is better for the rest of
your program to have the data in a true 2-d array, then I'd go with this
solution. But if you need to access the columns like that in many places,
then you're probably better off changing your data structure as the other
person described.
 
My advice all depends upon what you plan to do with the X and Y values,
but you are doing charting, then I wonder why you aren't using the
System.Drawing.Point structure and making an aggregate of that:

class MyClass
{
private ArrayList coordinates = new ArrayList();

public void Add(double[] XValues, double[] YValues)
{
int coordinateCount = Math.Min(XValues.Length, YValues.Length);
for (int i = 0; i < coordinateCount; i++)
{
this.coordinates.Add(new Point(XValues, YValues));
}
}
}

If you do things this way, you can do this:

Point aPoint = (Point)this.coordinates;

and then aPoint.X and aPoint.Y rather than remembering that index 0 is
X and index 1 is Y.

In .NET V2.0, which has generics, you can declare:

private List<System.Drawing.Point> coordinates = new
List<System.Drawing.Point>();

and then you can collapse the reference down to one line:

this.coordinates.X or this.coordinates.Y

Anyway, maybe not what you're after, but something to think about. :)
 
Back
Top