Questions tagged [multidimensional-array]

Multidimensional-arrays can be described as multi-dimensional tables. Each index used in order to find a given element is called a dimension.

An array is typically a data structure meant to store a collection of elements to which is associated a given index. These elements are often in contiguous memory locations although this is not a requirement.

Multidimensional-arrays can be described as multi-dimensional tables. Each index used in order to find a given element is called a dimension.

Depending on the programming language, such a data structure can be achieved through a specific multi-dimension array data structure or in the shape of an "array of arrays" (also known as jagged arrays or Iliffe vector).

Multidimensional-arrays are often used to put values in a table format (e.g. rows and columns).

Tutorials:

This StackOverflow thread provides some insight on the difference between multi-dimensional arrays and jagged arrays in C#.

Wikipedia article.

35247 questions
470
votes
13 answers

Syntax for creating a two-dimensional array in Java

Consider: int[][] multD = new int[5][]; multD[0] = new int[10]; Is this how you create a two-dimensional array with 5 rows and 10 columns? I saw this code online, but the syntax didn't make sense.
AppSensei
  • 8,270
  • 22
  • 71
  • 99
442
votes
19 answers

Passing a 2D array to a C++ function

I have a function which I want to take, as a parameter, a 2D array of variable size. So far I have this: void myFunction(double** myArray){ myArray[x][y] = 5; etc... } And I have declared an array elsewhere in my code: double…
RogerDarwin
  • 4,423
  • 3
  • 14
  • 4
430
votes
3 answers

What is the difference between flatten and ravel functions in numpy?

import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the same list. Then what is the need of two…
cryptomanic
  • 5,986
  • 3
  • 18
  • 30
424
votes
8 answers

Difference between numpy.array shape (R, 1) and (R,)

In numpy, some of the operations return in shape (R, 1) but some return (R,). This will make matrix multiplication more tedious since explicit reshape is required. For example, given a matrix M, if we want to do numpy.dot(M[:,0], numpy.ones((1, R)))…
clwen
  • 20,004
  • 31
  • 77
  • 94
379
votes
31 answers

How to initialize a two-dimensional array in Python?

I'm beginning python and I'm trying to use a two-dimensional list, that I initially fill up with the same variable in every place. I came up with this: def initialize_twodlist(foo): twod_list = [] new = [] for i in range (0, 10): …
thepandaatemyface
  • 5,034
  • 6
  • 25
  • 30
374
votes
5 answers

What is the difference between ndarray and array in NumPy?

What is the difference between ndarray and array in NumPy? Where is their implementation in the NumPy source code?
flxb
  • 4,235
  • 3
  • 16
  • 11
372
votes
31 answers

How to Flatten a Multidimensional Array?

Is it possible, in PHP, to flatten a (bi/multi)dimensional array without using recursion or references? I'm only interested in the values so the keys can be ignored, I'm thinking in the lines of array_map() and array_values().
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
370
votes
5 answers

How do I use np.newaxis?

What is numpy.newaxis and when should I use it? Using it on a 1-D array x produces: >>> x array([0, 1, 2, 3]) >>> x[np.newaxis, :] array([[0, 1, 2, 3]]) >>> x[:, np.newaxis] array([[0], [1], [2], [3]])
357
votes
64 answers

How do you rotate a two dimensional array?

Inspired by Raymond Chen's post, say you have a 4x4 two dimensional array, write a function that rotates it 90 degrees. Raymond links to a solution in pseudo code, but I'd like to see some real world…
swilliams
  • 48,060
  • 27
  • 100
  • 130
349
votes
8 answers

Understanding NumPy's einsum

How does np.einsum work? Given arrays A and B, their matrix multiplication followed by transpose is computed using (A @ B).T, or equivalently, using: np.einsum("ij, jk -> ki", A, B)
Lance Strait
  • 4,001
  • 4
  • 17
  • 18
320
votes
20 answers

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

Does anybody know how to extract a column from a multi-dimensional array in Python?
jaweria
287
votes
24 answers

in_array() and multidimensional array

I use in_array() to check whether a value exists in an array like below, $a = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $a)) { echo "Got Irix"; } //print_r($a); but what about an multidimensional array (below) - how can I…
Run
  • 54,938
  • 169
  • 450
  • 748
268
votes
6 answers

How do you get the width and height of a multi-dimensional array?

I have an array defined: int [,] ary; // ... int nArea = ary.Length; // x*y or total area This is all well and good, but I need to know how wide this array is in the x and y dimensions individually. Namely, ary.Length might return 12 - but does…
Giffyguy
  • 20,378
  • 34
  • 97
  • 168
214
votes
6 answers

How to sort an array of objects with jquery or javascript

I have an array of objects: var array = [(id, name, value),(id, name, value)]; //and so on How do I get the array to be sorted in ascending order of the atribute name (array[i][1])? I've tried to do this: array[i][1].sort(), but that doesn't…
Karoline Brynildsen
  • 3,598
  • 6
  • 35
  • 45
203
votes
10 answers

How do I Sort a Multidimensional Array in PHP

I have CSV data loaded into a multidimensional array. In this way each "row" is a record and each "column" contains the same type of data. I am using the function below to load my CSV file. function f_parse_csv($file, $longest, $delimiter) { …
Melikoth
  • 2,446
  • 2
  • 18
  • 16