开发者

Getting a double[] row array of a double[,] rectangular array

开发者 https://www.devze.com 2023-01-02 02:56 出处:网络
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:

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 a开发者_如何学编程pproach?

Thanks!


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);


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).


Why not make a generic extension method?

    public static T[] GetRow<T>(this T[,] input2DArray, int row) where T : IComparable
    {
        var width = input2DArray.GetLength(0);
        var height = input2DArray.GetLength(1);

        if (row >= height)
            throw new IndexOutOfRangeException("Row Index Out of Range");
        // Ensures the row requested is within the range of the 2-d array


        var returnRow = new T[width];
        for(var i = 0; i < width; i++)
            returnRow[i] = input2DArray[i, row];

        return returnRow;
    }

Like this all you have to code is:

array2D = new double[,];
// ... fill array here
var row = array2D.GetRow(4) // Implies getting 5th row of the 2-D Array

This is useful if you're trying to chain methods after obtaining a row and could be helpful with LINQ commands as well.


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 Dynamically created jagged rectangular array


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;
}


Although this is an old thread, an addition to Joseph Sturtevants answer may be useful. His function crashes in case the matrix's first column is not zero, but another integer. This is e.g. always the case in case of retrieving data from Excel, like

object[,] objects = null;
Excel.Range range = worksheet.get_Range("A1", "C5");
objects = range.Cells.Value; //columns start at 1, not at 0

The GetRow function could be modified like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号