By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,537 Members | 873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,537 IT Pros & Developers. It's quick & easy.

Converting a multidimensional array to 2 single dimensional arrays

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

Can you use jagged arrays (i.e. arrays of arrays), i.e. can you alter the
definition of your multidimensional array into "private double[][] myArray"
? With jagged arrays you can get the columns (rows) at once. See for an
example on
http://msdn.microsoft.com/library/de...ysTutorial.asp

Regards,
Bart

--
http://www.xenopz.com
"Claire" <cc@hotmai1.com> wrote in message
news:39*************@individual.net...
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

Nov 16 '05 #2

P: n/a
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[i] = myArray[0,i];
YValues[i] = 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.
"Claire" <cc@hotmai1.com> wrote in message
news:39*************@individual.net...
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

Nov 16 '05 #3

P: n/a
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[i], YValues[i]));
}
}
}

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

Point aPoint = (Point)this.coordinates[i];

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[i].X or this.coordinates[i].Y

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

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.