tags:

views:

116

answers:

4

Suppose you have an array like:

double[,] rectArray = new double[10,3];

Now you want the fouth row as a double[] array of 3 elements without doing:

double[] fourthRow = new double[]{rectArray[3,0],
                                  rectArray[3,1], 
                                  rectArray[3,2]};

Is it possible someway? Even using a Marshal.Something approach?

Thanks!

+1  A: 

You probably want to use a jagged array. That is not an array of 10 by 3 but instead an array of arrays.

Something like :

        double[][] rectArray;
         ....
        double [] rowArray = rectArray[3];

There are lots of places to learn more about jagged arrays. For example http://stackoverflow.com/questions/2458486/dynamically-created-jagged-rectangular-array

Hogan
I need a rectangular array, sorry.
devdept
Ok... but you describe a task you want to do that makes more sense with a jagged array. (Which are often faster and smaller anyways.)
Hogan
-1: Didn't answer the question.
AMissico
@AMissico : lol, revenge and sniping don't go very far on SO, kinda misses the whole point. SO is about working together toward a common goal and engaging people as peers. Fighting the meta fight that does not exist is kinda a waste of everyone's time.
Hogan
@Hogan: I do not understand what you are talking about or referring to.
AMissico
@AMissico: Really? Ok, I'll explain: Since my answer did answer the question (or approximated an answer; max's is clearly better) then I have to assume your down-vote and comment were in retaliation to my down-vote and comment to your (now deleted) post. My comment on your answer was exactly the same. I've no interest in continuing this thread. IMO "trolling" and "flame-wars" are unproductive on SO, and I'm stopping my participation here.
Hogan
@Hogan: Sorry. I think you are confusing me with someone else. I did not answer this question because I am unsure whether Block.BlockCopy is what is needed and I do not fully understand the question. Yet, it is clear the questioner has a requirement to use [,] and suggesting someone replace an implementation is not an answer. Therefore, my down vote and comment to explain why.
AMissico
+4  A: 

You can use Buffer.BlockCopy method:

const int d1 = 10;
const int d2 = 3;
const int doubleSize = 8;

double[,] rectArray = new double[d1, d2];
double[] target = new double[d2];

int rowToGet = 3;
Buffer.BlockCopy(rectArray, doubleSize * d2 * rowToGet, target, 0, doubleSize * d2);
max
A: 

If you must use a rectangular array and just want to simplify the syntax, you can use a method to get the row like so:

double[] fourthRow = GetRow(rectArray, 3);

public static T[] GetRow<T>(T[,] matrix, int row)
{
    var columns = matrix.GetLength(1);
    var array = new T[columns];
    for (int i = 0; i < columns; ++i)
        array[i] = matrix[row, i];
    return array;
}
Joseph Sturtevant
+1  A: 

LINQ to the rescue:

var s = rectArray.Cast<double>().Skip(9).Take(3).ToArray();

Explanation: Casting a multi-dimensional array flattens it to a single-dimensional array. After that all we need to do is skip to the element we want (the 4th element in the 2-D array resolves to Skip(9)...) and take 3 elements from it).

code4life
@code4life: I like this approach but it is not fast enough for our purposes, sorry.
devdept