Getting the array length of a 2D array in Java

Consider public static void main(String[] args) { int[][] foo = new int[][] { new int[] { 1, 2, 3 }, new int[] { 1, 2, 3, 4}, }; System.out.println(foo.length); //2 System.out.println(foo[0].length); //3 System.out.println(foo[1].length); //4 } Column lengths differ per row. If you’re backing some data by a fixed size 2D array, then provide getters to … Read more

Using numpy to build an array of all combinations of two arrays

In newer version of numpy (>1.8.x), numpy.meshgrid() provides a much faster implementation: @pv’s solution In [113]: %timeit cartesian(([1, 2, 3], [4, 5], [6, 7])) 10000 loops, best of 3: 135 µs per loop In [114]: cartesian(([1, 2, 3], [4, 5], [6, 7])) Out[114]: array([[1, 4, 6], [1, 4, 7], [1, 5, 6], [1, 5, 7], … Read more

How do I Sort a Multidimensional Array in PHP [duplicate]

Introducing: a very generalized solution for PHP 5.3+ I ‘d like to add my own solution here, since it offers features that other answers do not. Specifically, advantages of this solution include: It’s reusable: you specify the sort column as a variable instead of hardcoding it. It’s flexible: you can specify multiple sort columns (as … Read more

in_array() and multidimensional array

in_array() does not work on multidimensional arrays. You could write a recursive function to do that for you: function in_array_r($needle, $haystack, $strict = false) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; } Usage: … Read more

How do you extract a column from a multi-dimensional array?

>>> import numpy as np >>> A = np.array([[1,2,3,4],[5,6,7,8]]) >>> A array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> A[:,2] # returns the third columm array([3, 7]) See also: “numpy.arange” and “reshape” to allocate memory Example: (Allocating a array with shaping of matrix (3×4)) nrows = 3 ncols = 4 my_array = numpy.arange(nrows*ncols, dtype=”double”) … Read more