Skip to main content

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.

multidimensional-array
Filter by
Sorted by
Tagged with
5371 votes
34 answers
4.4m views

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 ...
Emma's user avatar
  • 54.5k
1565 votes
88 answers
1.3m views

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's user avatar
  • 19.1k
1409 votes
17 answers
1.2m views

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 ( [...
stef's user avatar
  • 27.4k
1399 votes
56 answers
2.8m views

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? (...
Diego's user avatar
  • 17.1k
911 votes
30 answers
3.0m views

How to define a two-dimensional array? [duplicate]

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's user avatar
678 votes
23 answers
552k views

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 [...
Alex's user avatar
  • 67.5k
651 votes
32 answers
1.1m views

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's user avatar
  • 7,215
645 votes
30 answers
1.3m views

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: ...
user20844's user avatar
  • 6,627
610 votes
23 answers
758k views

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 ...
Matt's user avatar
  • 22.7k
581 votes
18 answers
540k views

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"...
ncvncvn's user avatar
  • 5,843
522 votes
5 answers
762k views

Multidimensional Array [][] vs [,] [duplicate]

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? ...
william007's user avatar
  • 18.2k
519 votes
13 answers
166k views

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's user avatar
  • 11.8k
515 votes
5 answers
143k views

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<T> since C++98 and std::array<T, n> ...
fredoverflow's user avatar
498 votes
9 answers
361k views

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, ...
HonzaB's user avatar
  • 7,285
477 votes
23 answers
836k views

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', ...
Rachit's user avatar
  • 4,809
475 votes
13 answers
1.7m views

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's user avatar
  • 8,370
451 votes
19 answers
1.1m views

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 ...
RogerDarwin's user avatar
  • 4,513
447 votes
3 answers
135k views

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 ...
cryptomanic's user avatar
  • 6,216
434 votes
8 answers
239k views

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 ...
clwen's user avatar
  • 20.6k
394 votes
31 answers
1.2m views

How to initialize a two-dimensional array (list of lists, if not using NumPy) 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 = []...
thepandaatemyface's user avatar
394 votes
5 answers
208k views

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's user avatar
  • 4,465
383 votes
5 answers
482k views

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....
Yue Harriet Huang's user avatar
378 votes
31 answers
440k views

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 ...
Alix Axel's user avatar
  • 154k
367 votes
64 answers
410k views

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 ...
366 votes
8 answers
179k views

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's user avatar
  • 4,191
326 votes
20 answers
1.1m views

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?
user avatar
288 votes
24 answers
474k views

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 ...
Run's user avatar
  • 56.5k
283 votes
6 answers
281k views

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

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. ...
Giffyguy's user avatar
  • 21.1k
215 votes
6 answers
332k views

How to sort an array of objects with jquery or javascript [duplicate]

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 ...
Karoline Brynildsen's user avatar
211 votes
10 answers
264k views

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

I'm trying to run over the parameters space of a six-parameter function to study its numerical behavior before trying to do anything complex with it, so I'm searching for an efficient way to do this. ...
Rafael S. Calsaverini's user avatar
205 votes
10 answers
202k views

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

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 ...
Melikoth's user avatar
  • 2,466
195 votes
10 answers
599k views

Convert a 1D array to a 2D array in numpy

I want to convert a 1-dimensional array into a 2-dimensional array by specifying the number of columns in the 2D array. Something that would work like this: > import numpy as np > A = np.array([...
digbyterrell's user avatar
  • 3,579
186 votes
9 answers
170k views

Rotating a two-dimensional array in Python

In a program I'm writing the need to rotate a two-dimensional array came up. Searching for the optimal solution I found this impressive one-liner that does the job: rotated = zip(*original[::-1]) I'...
paldepind's user avatar
  • 4,860
173 votes
4 answers
703k views

How to access first level keys of a 2d array with a foreach loop? [duplicate]

How do I access the first level key of a two-dimensional array using a foreach loop? I have a $places array like this: [Philadelphia] => Array ( [0] => Array (...
matthewb's user avatar
  • 3,480
171 votes
13 answers
788k views

Getting the array length of a 2D array in Java

I need to get the length of a 2D array for both the row and column. I’ve successfully done this, using the following code: public class MyClass { public static void main(String args[]) { int[...
user432209's user avatar
  • 20.1k
167 votes
16 answers
132k views

Checking if array is multidimensional or not?

What is the most efficient way to check if an array is a flat array of primitive values or if it is a multidimensional array? Is there any way to do this without actually looping through an array and ...
Wilco's user avatar
  • 33.1k
165 votes
6 answers
296k views

How to re-index all subarray elements of a multidimensional array?

The question is how to reset key e.g. for an array: Array ( [1_Name] => Array ( [1] => leo [4] => NULL ) [1_Phone] => Array ( [1] => 12345 ...
Leo Chan's user avatar
  • 4,407
161 votes
19 answers
179k views

Matrix Transpose in Python [duplicate]

I am trying to create a matrix transpose function for python but I can't seem to make it work. Say I have theArray = [['a','b','c'],['d','e','f'],['g','h','i']] and I want my function to come up ...
Julio Diaz's user avatar
  • 9,297
161 votes
6 answers
460k views

Loop a multidimensional array and only print two specific column values per row

How can I print the filepath and filename values from each row? Array ( [0] => Array ( [fid] => 14 [list] => 1 [data] => Array ( ...
esafwan's user avatar
  • 17.7k
160 votes
15 answers
557k views

How to create multidimensional array

Can anyone give me a sample/example of JavaScript with a multidimensional array of inputs? Hope you could help because I'm still new to the JavaScript. Like when you input 2 rows and 2 columns the ...
SpitFire's user avatar
  • 1,631
153 votes
2 answers
3k views

Why is the enumeration value from a multi dimensional array not equal to itself?

Consider: using System; public class Test { enum State : sbyte { OK = 0, BUG = -1 } static void Main(string[] args) { var s = new State[1, 1]; s[0, 0] = State.BUG; ...
shingo's user avatar
  • 23.7k
150 votes
4 answers
301k views

Selecting specific rows and columns from NumPy array

I've been going crazy trying to figure out what stupid thing I'm doing wrong here. I'm using NumPy, and I have specific row indices and specific column indices that I want to select from. Here's the ...
Mike C's user avatar
  • 2,019
149 votes
18 answers
304k views

How to sum all column values in multi-dimensional array?

How can I add all the columnar values by associative key? Note that key sets are dynamic. Input array: Array ( [0] => Array ( [gozhi] => 2 [uzorong] => ...
marknt15's user avatar
  • 5,097
141 votes
10 answers
296k views

PHP Multidimensional Array Searching (Find key by specific value)

I have this multidimensional array. I need to search it and return only the key that matches the value of the "slug". I know there are other threads about searching multidimensional arrays, but I'm ...
Ben Kouba's user avatar
  • 1,413
140 votes
14 answers
76k views

PHP best way to MD5 multi-dimensional array?

What is the best way to generate an MD5 (or any other hash) of a multi-dimensional array? I could easily write a loop which would traverse through each level of the array, concatenating each value ...
Peter John's user avatar
  • 1,879
137 votes
3 answers
83k views

Argmax of numpy array returning non-flat indices

I'm trying to get the indices of the maximum element in a Numpy array. This can be done using numpy.argmax. My problem is, that I would like to find the biggest element in the whole array and get the ...
Andreas Mueller's user avatar
135 votes
9 answers
161k views

PHP - Merging two arrays into one array (also Remove Duplicates)

Hi I'm Trying to merge two arrays and also want to remove duplicate values from final Array. Here is my Array 1: Array ( [0] => stdClass Object ( [ID] => 749 [post_author] =...
Ravikumar Sharma's user avatar
127 votes
16 answers
274k views

How to sort 2 dimensional array by column value?

Can any one help me sort a 2 dimensional Array in JavaScript? It will have data in the following format: [12, AAA] [58, BBB] [28, CCC] [18, DDD] It should look like this when sorted: [12, AAA] [18,...
Alex's user avatar
  • 12k
125 votes
9 answers
198k views

Javascript Map Array Last Item

I have this: map = ranks.map((row, r) => ( row.map((rank, i) => { return [element(r, i, state, rank, toggled, onClick)]; }) )); It maps through a 2-dimentional array. After each row, I'...
cocacrave's user avatar
  • 2,573
123 votes
14 answers
473k views

The best way to print a Java 2D array? [closed]

I was wondering what the best way of printing a 2D array in Java was? I was just wondering if this code is good practice or not? Also any other mistakes I made in this code if you find any. int rows = ...
Chip Goon Lewin's user avatar

1
2 3 4 5
715