Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a dimension (slice) from a multidimensional array

I'm trying to figure out how to get a single dimension from a multidimensional array (for the sake of argument, let's say it's 2D), I have a multidimensional array:

double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } }; 

If it was a jagged array, I would simply call d[0] and that would give me an array of {1, 2, 3, 4, 5}, is there a way I can achieve the same with a 2D array?

like image 499
Kiril Avatar asked Jan 26 '11 06:01

Kiril


People also ask

How do you slice a multi dimensional array in Python?

I've figured out how to slice 1 dimensional sequence: arr[start:end] , and access an element in the array: el = arr[row][col] . Trying something like slice = arr[0:2][0:2] (where arr is a numpy array) doesn't give me the first 2 rows and columns, but repeats the first 2 rows.

How do you find the dimensions of a 2D array?

We use arrayname. length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has. The number of columns may vary row to row, which is why the number of rows is used as the length of the 2D array.


2 Answers

No. You could of course write a wrapper class that represents a slice, and has an indexer internally - but nothing inbuilt. The other approach would be to write a method that makes a copy of a slice and hands back a vector - it depends whether you want a copy or not.

using System; static class ArraySliceExt {     public static ArraySlice2D<T> Slice<T>(this T[,] arr, int firstDimension)     {         return new ArraySlice2D<T>(arr, firstDimension);     } } class ArraySlice2D<T> {     private readonly T[,] arr;     private readonly int firstDimension;     private readonly int length;     public int Length { get { return length; } }     public ArraySlice2D(T[,] arr, int firstDimension)     {         this.arr = arr;         this.firstDimension = firstDimension;         this.length = arr.GetUpperBound(1) + 1;     }     public T this[int index]     {         get { return arr[firstDimension, index]; }         set { arr[firstDimension, index] = value; }     } } public static class Program {     static void Main()     {         double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } };         var slice = d.Slice(0);         for (int i = 0; i < slice.Length; i++)         {             Console.WriteLine(slice[i]);         }     } } 
like image 117
Marc Gravell Avatar answered Oct 02 '22 13:10

Marc Gravell


Improved version of that answer:

public static IEnumerable<T> SliceRow<T>(this T[,] array, int row) {     for (var i = array.GetLowerBound(1); i <= array.GetUpperBound(1); i++)     {         yield return array[row, i];     } }  public static IEnumerable<T> SliceColumn<T>(this T[,] array, int column) {     for (var i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)     {         yield return array[i, column];     } } 
like image 32
tsul Avatar answered Oct 02 '22 13:10

tsul