P
per9000
Hi all,
I have a two-dimensional array of data, f.x int's. We can imagine that
the array is "really large". Now I want the data in it and store this
in a one-dimensional array.
The obvious way to do this is a nested for-loop - but we all know
O(n^2) is bad. So I am looking for something like ArrayList.ToArray(),
or Matlabs A).
C#
// D is an ArrayList
D.ToArray();
Matlab
% A is a matrix
A)
I tried a cast that could have been perfect for at least one of the
cases.
((int[])A).CopyTo(D, 0);
But I learn that I "Cannot convert type 'int[*,*]' to 'int[]'"
Another problem here is that I sometimes want the data rowbased -
sometimes columnbased.
Right now I do something like the below code, that is horribly
2*O(n^2), I guess I could perhaps live with 1*O(n^2), but any shortcuts
are welcome.
Thanks,
Per
//
// sample code below
//
static void Main(string[] args)
{
int dim1 = 2;
int dim2 = 4;
int k = 0;
// create arrays
int[,] A = new int[dim1 , dim2];
int[] B = new int[dim1 * dim2];
int[] C = new int[dim1 * dim2];
// fill A with silly values
for (int i = 0; i < A.GetLength(0); i++)
for (int j = 0; j < A.GetLength(1); j++)
A[i, j] = k++;
//
// copy from A to B - rowbased
// O(N^2)
//
int idx = 0;
for (int i = 0; i < A.GetLength(0); i++)
for (int j = 0; j < A.GetLength(1); j++)
B[idx++] = A[i, j];
//
// copy from A to C - column-based
// O(N^2)
//
idx = 0;
for (int j = 0; j < A.GetLength(1); j++)
for (int i = 0; i < A.GetLength(0); i++)
C[idx++] = A[i, j];
Console.WriteLine("A:");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
Console.Write(" {0}", A[i, j]);
Console.WriteLine();
}
Console.WriteLine("B:");
foreach (double b in B)
Console.Write(" {0}", b);
Console.WriteLine();
Console.WriteLine("C:");
foreach (double c in C)
Console.Write(" {0}", c);
Console.WriteLine();
}
I have a two-dimensional array of data, f.x int's. We can imagine that
the array is "really large". Now I want the data in it and store this
in a one-dimensional array.
The obvious way to do this is a nested for-loop - but we all know
O(n^2) is bad. So I am looking for something like ArrayList.ToArray(),
or Matlabs A).
C#
// D is an ArrayList
D.ToArray();
Matlab
% A is a matrix
A)
I tried a cast that could have been perfect for at least one of the
cases.
((int[])A).CopyTo(D, 0);
But I learn that I "Cannot convert type 'int[*,*]' to 'int[]'"
Another problem here is that I sometimes want the data rowbased -
sometimes columnbased.
Right now I do something like the below code, that is horribly
2*O(n^2), I guess I could perhaps live with 1*O(n^2), but any shortcuts
are welcome.
Thanks,
Per
//
// sample code below
//
static void Main(string[] args)
{
int dim1 = 2;
int dim2 = 4;
int k = 0;
// create arrays
int[,] A = new int[dim1 , dim2];
int[] B = new int[dim1 * dim2];
int[] C = new int[dim1 * dim2];
// fill A with silly values
for (int i = 0; i < A.GetLength(0); i++)
for (int j = 0; j < A.GetLength(1); j++)
A[i, j] = k++;
//
// copy from A to B - rowbased
// O(N^2)
//
int idx = 0;
for (int i = 0; i < A.GetLength(0); i++)
for (int j = 0; j < A.GetLength(1); j++)
B[idx++] = A[i, j];
//
// copy from A to C - column-based
// O(N^2)
//
idx = 0;
for (int j = 0; j < A.GetLength(1); j++)
for (int i = 0; i < A.GetLength(0); i++)
C[idx++] = A[i, j];
Console.WriteLine("A:");
for (int i = 0; i < A.GetLength(0); i++)
{
for (int j = 0; j < A.GetLength(1); j++)
Console.Write(" {0}", A[i, j]);
Console.WriteLine();
}
Console.WriteLine("B:");
foreach (double b in B)
Console.Write(" {0}", b);
Console.WriteLine();
Console.WriteLine("C:");
foreach (double c in C)
Console.Write(" {0}", c);
Console.WriteLine();
}