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
5192
votes
33 answers

How do I make a flat list out of a list of lists?

I have a list of lists like [ [1, 2, 3], [4, 5, 6], [7], [8, 9] ] How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]? If your list of lists comes from a nested list comprehension, the problem can be solved more simply/directly…
Emma
  • 52,713
  • 4
  • 19
  • 10
1532
votes
87 answers

Merge/flatten an array of arrays

I have a JavaScript array like: [["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]] How would I go about merging the separate inner arrays into one like: ["$6", "$12", "$25", ...]
Andy
  • 18,723
  • 12
  • 46
  • 54
1381
votes
55 answers

How can I create a two dimensional array in JavaScript?

I have been reading online and some places say it isn't possible, some say it is and then give an example and others refute the example, etc. How do I declare a 2 dimensional array in JavaScript? (assuming it's possible) How would I access its…
Diego
  • 16,830
  • 8
  • 34
  • 46
1379
votes
17 answers

How to Sort a Multi-dimensional Array by Value

How can I sort this array by the value of the "order" key? Even though the values are currently sequential, they will not always be. Array ( [0] => Array ( [hashtag] => a7e87329b5eab8578f4f1098a152d6f4 [title] =>…
stef
  • 26,771
  • 31
  • 105
  • 143
891
votes
30 answers

How to define a two-dimensional array?

I want to define a two-dimensional array without an initialized length like this: Matrix = [][] But this gives an error: IndexError: list index out of range
Masoud Abasian
  • 10,549
  • 6
  • 23
  • 22
657
votes
23 answers

Sort array of objects by one property

How can I sort this array of objects by one of its fields, like name or count? Array ( [0] => stdClass Object ( [ID] => 1 [name] => Mary Jane [count] => 420 ) [1] => stdClass Object …
Alex
  • 66,732
  • 177
  • 439
  • 641
635
votes
29 answers

How do I declare a 2d array in C++ using new?

How do i declare a 2d array using new? Like, for a "normal" array I would: int* ary = new int[Size] but int** ary = new int[sizeY][sizeX] a) doesn't work/compile and b) doesn't accomplish what: int ary[sizeY][sizeX] does.
user20844
  • 6,527
  • 4
  • 18
  • 9
617
votes
32 answers

How do I count the occurrence of a certain item in an ndarray?

How do I count the number of 0s and 1s in the following array? y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]) y.count(0) gives: numpy.ndarray object has no attribute count
mflowww
  • 6,835
  • 6
  • 18
  • 18
602
votes
23 answers

Sort an array of associative arrays by column value

Given this array: $inventory = array( array("type"=>"fruit", "price"=>3.50), array("type"=>"milk", "price"=>2.90), array("type"=>"pork", "price"=>5.43), ); I would like to sort $inventory's elements by price to get: $inventory = array( …
Matt
  • 22,224
  • 25
  • 80
  • 116
565
votes
18 answers

How do you add an array to another array in Ruby and not end up with a multi-dimensional result?

I tried: somearray = ["some", "thing"] anotherarray = ["another", "thing"] somearray.push(anotherarray.flatten!) I expected ["some", "thing", "another", "thing"] but got ["some", "thing", nil]
ncvncvn
  • 5,683
  • 2
  • 16
  • 4
513
votes
5 answers

How do I use arrays in C++?

C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector since C++98 and std::array since C++11), so the need for arrays does not arise quite as…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
512
votes
12 answers

Differences between a multidimensional array "[,]" and an array of arrays "[][]" in C#?

What are the differences between multidimensional arrays double[,] and array of arrays double[][] in C#? If there is a difference? What is the best use for each one?
ecleel
  • 11,748
  • 15
  • 48
  • 48
511
votes
5 answers

Multidimensional Array [][] vs [,]

double[][] ServicePoint = new double[10][9]; // <-- gives an error (1) double[,] ServicePoint = new double[10,9]; // <-- ok (2) What's their difference? (1) yields an error, what's the reason? And double d = new double[9] ServicePoint[0] =…
william007
  • 17,375
  • 25
  • 118
  • 194
487
votes
9 answers

What is the purpose of meshgrid in NumPy?

What is the purpose of np.meshgrid? I know it creates some kind of grid of coordinates for plotting, but I can't see the direct benefit of it. The official documentation gives the following example, but its output doesn't make sense to me: x =…
HonzaB
  • 7,065
  • 6
  • 31
  • 42
471
votes
23 answers

PHP multidimensional array search by value

I have an array where I want to search the uid and get the key of the array. Examples Assume we have the following 2-dimensional array: $userdb = array( array( 'uid' => '100', 'name' => 'Sandra Shush', 'pic_square' =>…
Rachit
  • 4,749
  • 3
  • 16
  • 7
1
2 3
99 100