How to get an array of 1D columns and an array of 1D rows from a 2D array? (C # .NET)

i double[,] Array; . Is it possible to get something like double[] ColumnArray0 = Array[0,].toArray() and double[] RowArray1 = Array[,1].toArray() without making a copy of each element (using for)?

Thanks.

+6
source share
2 answers

Arrays are a region of memory in which all records are stored sequentially. Depending on the location of the data in memory, this is only possible for rows or columns.

Instead of type 2d array double[,] in your case it is better to use an array of arrays double[][]

 double[][] Array2d = new double[10][]; Array2d[0] = new double[10]; Array2d[1] = new double[10]; ... and then: double[] RowArray0 = Array2d[0]; 

Depending on how you put the data in your array, you can also consider Array2d as an array of columns. But to have both at the same time is impossible.

See also here: Multidimensional array [] [] vs [,]

+2
source

Although I am very late, I want to give an alternative answer to the question.

The first important part of the question was to be able to access the full rows OR columns of the matrix. One way to do this is to use extension methods:

 public static class MatrixExtensions { /// <summary> /// Returns the row with number 'row' of this matrix as a 1D-Array. /// </summary> public static T[] GetRow<T>(this T[,] matrix, int row) { var rowLength = matrix.GetLength(1); var rowVector = new T[rowLength]; for (var i = 0; i < rowLength; i++) rowVector[i] = matrix[row, i]; return rowVector; } /// <summary> /// Sets the row with number 'row' of this 2D-matrix to the parameter 'rowVector'. /// </summary> public static void SetRow<T>(this T[,] matrix, int row, T[] rowVector) { var rowLength = matrix.GetLength(1); for (var i = 0; i < rowLength; i++) matrix[row, i] = rowVector[i]; } /// <summary> /// Returns the column with number 'col' of this matrix as a 1D-Array. /// </summary> public static T[] GetCol<T>(this T[,] matrix, int col) { var colLength = matrix.GetLength(0); var colVector = new T[colLength]; for (var i = 0; i < colLength; i++) colVector[i] = matrix[i, col]; return colVector; } /// <summary> /// Sets the column with number 'col' of this 2D-matrix to the parameter 'colVector'. /// </summary> public static void SetCol<T>(this T[,] matrix, int col, T[] colVector) { var colLength = matrix.GetLength(0); for (var i = 0; i < colLength; i++) matrix[i, col] = colVector[i]; } } 

Usage example:

 double[,] myMatrix = ... // Initialize with desired size and values. double[] myRowVector = myMatrix.GetRow(2); // Gets the third row. double[] myColVector = myMatrix.GetCol(1); // Gets the second column. myMatrix.SetCol(2, myColVector); // Sets the third column to the second column. 

First of all, it should be noted that you can use these general methods with any [,] - matrices and corresponding [] -vectors. Imagine replacing T with double s, and you will get a specific version for "double" (as OP was asked).

Secondly, getting and setting up rows uses Array.Copy , and when getting and setting columns, a loop is used. This is due to the C # string order , which allows you to use the first, but not the second. Of course, both can be implemented using a loop, as can be seen from the comments.

Be sure to pass the correct sizes for set-Methods, or the program will fail (error and size checking can be easily added). All logic can also be implemented for gear arrays, such as double[][] , but the OP specifically requested multidimensional ones.

As for the second part of the question: if your matrix consists of double and double is a value type, the values ​​will always be copied. Therefore, your desired behavior of not copying values ​​would be impossible. However, if you use objects like T , only the link pointing to the object will be copied, not the object itself (therefore, you should not mutate the "copied" object).

Finally, if you really do not want to copy double values, I would suggest passing your entire matrix (only this link is passed), and then directly sorting through the columns and / or rows you need.

+3
source

Source: https://habr.com/ru/post/945367/