I have a long (10000+ entries) List of structs, each of which contain an int[16]. I want to transpose the data to create 16 int[] arrays which are 10000+ entries long. Essentially, I want to transpose the data. Is there any method faster than just iterating through the list and creating the new entries?
Asked
Active
Viewed 171 times
1
-
This link might help http://stackoverflow.com/questions/4801990/how-to-get-a-dimension-slice-from-a-multidimensional-array – Mihai Nov 09 '12 at 10:24
-
2If you can post the code you have already, people could suggest optimisations, otherwise from your description above it doesn't sound as though there would be a faster way. The arrays would need to be created and the data copied, there's no way around doing the minimum that needs to happen. – Adrian Thompson Phillips Nov 09 '12 at 10:27
-
Faster in what meaning ? Less code, higher performance ? – jwaliszko Nov 09 '12 at 10:30
-
1Is it possible that you don't need a physical copy of the arrays, just an easier way to access the `int` values with the indices the other way around? – Rawling Nov 09 '12 at 10:36
2 Answers
0
So you just want a array of the int[16]?
Maybe linq could do this
var newArray = fooArray.Select(foo => foo.IntVal).ToArray();

sa_ddam213
- 42,848
- 7
- 101
- 110
0
Something like this should do you:
struct Widget
{
public int[] Values = new int[16];
}
public int[][] TransposeWidgets( List<Widget> widgets )
{
int cols;
int[][] result = new int[][];
for ( int row = 0 ; i < 16 ; ++row )
{
results[row] = new int[ widgets.Count ];
for ( int col = 0 ; col < widgets.Count ; ++col )
{
result[row][col] = widgets[col].Values[row];
}
}
}
Though it might be faster to iterate over the large list just once. If it's really large, iterating over that list could cause paging which would slow things down.

Nicholas Carey
- 71,308
- 16
- 93
- 135