Get Column from a Two Dimensional Array

How to get a complete row or column from 2D array in C#

You can optimise it for getting rows by using Buffer.BlockCopy(), but to get a column you'll have to loop. Buffer.BlockCopy() ultimately uses a processor instruction to copy a block of memory, so it is pretty fast.

It's convenient to put the code into an extension method to make it easier to call. Note that Buffer.BlockCopy() can only be used on arrays of primitive types, i.e. int, double, char etc. This does NOT include string.

Here's a compilable example:

using System;
using System.Linq;
using System.Runtime.InteropServices;

namespace ConsoleApplication4
{
public static class Program
{
private static void Main()
{
var array = new [,]
{
{0.1, 0.2, 0.3, 0.4, 0.5},
{1.1, 1.2, 1.3, 1.4, 1.5},
{2.1, 2.2, 2.3, 2.4, 2.5},
{3.1, 3.2, 3.3, 3.4, 3.5},
};

var row = array.GetRow(2);

// This prints 2.1, 2.2, 2.3, 2.4, 2.5

Console.WriteLine(string.Join(", ", row.Select(element => element.ToString())));
}
}

public static class ArrayExt
{
public static T[] GetRow<T>(this T[,] array, int row)
{
if (!typeof(T).IsPrimitive)
throw new InvalidOperationException("Not supported for managed types.");

if (array == null)
throw new ArgumentNullException("array");

int cols = array.GetUpperBound(1) + 1;
T[] result = new T[cols];

int size;

if (typeof(T) == typeof(bool))
size = 1;
else if (typeof(T) == typeof(char))
size = 2;
else
size = Marshal.SizeOf<T>();

Buffer.BlockCopy(array, row*cols*size, result, 0, cols*size);

return result;
}
}
}

Get column from a two dimensional array

You have to loop through each element in the 2d-array, and get the nth column.

    function getCol(matrix, col){
var column = [];
for(var i=0; i<matrix.length; i++){
column.push(matrix[i][col]);
}
return column;
}

var array = [new Array(20), new Array(20), new Array(20)]; //..your 3x20 array
getCol(array, 0); //Get first column

get columns from two dimensional array in java

There's no "out-of-the-box" way, but you can create a static method for this:

public static Object[] getColumn(Object[][] array, int index){
Object[] column = new Object[array[0].length]; // Here I assume a rectangular 2D array!
for(int i=0; i<column.length; i++){
column[i] = array[i][index];
}
return column;
}

How to extract columns from a 2D array?

You can make a function like this that selects columns based on an array of indices:

const getColumns = (arr, indices) => arr.map(row => indices.map(i => row[i]));
getColumns(arr, [0, 1]); // returns the first two columns

If the ultimate aim is to split the array into equal size chunks you could do it like this:

const splitIntoColumnGroups = (arr, width) => 
[...Array(Math.ceil(arr[0].length/width)).keys()].map(i =>
arr.map(row =>
row.slice(i * width, (i + 1) * width)));

How can i get a column from 2d array in java

A column has the same length as the number of rows, which is the number of elements in arr.

So change

Object[] column = new Object[arr[0].length];

to

Object[] column = new Object[arr.length];

How to get rows and columns count of a 2D array in Java?

Well you probably want array_name.length for getting the count of the rows and array_name[0].length for the columns. That is, if you defined your array like so:

T[][] array_name = new T[row][col];
array_name.length // row
array_name[0].length // col

How to extract multiple columns from a two dimension array?

You can .map() each row in arr to a new array which you can obtain by using an inner .map() on an array of columns/indexes which you wish to obtain. The inner map will map each index to its associated value from the row, giving you the values at each column for each row.

See example below:

const arr = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12],
];

const cols = [1, 2, 4];
const res = arr.map(r => cols.map(i => r[i-1]));
console.log(res);

How to get column of a multidimensional array in C/C++?

In C/C++, multidimensional arrays are actually stored as one dimensional arrays (in the memory). Your 2D matrix is stored as a one dimensional array with row-first ordering. That is why getting a column out of it is not easy, and not provided by default. There is no contiguous array in the memory that you can get a pointer to which represents a column of a multidimensional array. See below:

When you do p=matrix[0], you are just getting the pointer to the first element matrix[0][0], and that makes you think that you got the pointer to first row. Actually, it is a pointer to the entire contiguous array that holds matrix, as follows:

matrix[0][0]
matrix[0][1]
matrix[0][2]
.
.
matrix[1][0]
matrix[1][1]
matrix[1][2]
.
.
matrix[8][0]
matrix[8][1]
matrix[8][2]
.
.
matrix[8][8]

As seen above, the elements of any given column are separated by other elements in the corresponding rows.

So, as a side note, with pointer p, you can walk through the entire 81 elements of your matrix if you wanted to.



Related Topics



Leave a reply



Submit