multidimensional-array
Which comes first in a 2D array, rows or columns?
Java specifies arrays similar to that of a “row major” configuration, meaning that it indexes rows first. This is because a 2D array is an “array of arrays”. For example: int[ ][ ] a = new int[2][4]; // Two rows and four columns. a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] It can also be … Read more
Selecting specific rows and columns from NumPy array
As Toan suggests, a simple hack would be to just select the rows first, and then select the columns over that. >>> a[[0,1,3], :] # Returns the rows you want array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [12, 13, 14, 15]]) >>> a[[0,1,3], :][:, [0,2]] # Selects the columns you want as … Read more
Loop a multidimensional array and only print two specific column values per row
Using a foreach loop without a key: foreach($array as $item) { echo $item[‘filename’]; echo $item[‘filepath’]; // To know what’s in $item echo ‘<pre>’; var_dump($item); } Using a foreach loop with a key: foreach($array as $i => $item) { echo $array[$i][‘filename’]; echo $array[$i][‘filepath’]; // $array[$i] is same as $item } Using a for loop: for ($i … Read more
Differences between a multidimensional array “[,]” and an array of arrays “[][]” in C#?
Array of arrays (jagged arrays) are faster than multi-dimensional arrays and can be used more effectively. Multidimensional arrays have nicer syntax. If you write some simple code using jagged and multidimensional arrays and then inspect the compiled assembly with an IL disassembler you will see that the storage and retrieval from jagged (or single dimensional) … Read more
Sort an array of associative arrays by column value
You are right, the function you’re looking for is array_multisort(). Here’s an example taken straight from the manual and adapted to your case: $price = array(); foreach ($inventory as $key => $row) { $price[$key] = $row[‘price’]; } array_multisort($price, SORT_DESC, $inventory); As of PHP 5.5.0 you can use array_column() instead of that foreach: $price = array_column($inventory, … Read more
How to insert a new key value pair in array in php? [duplicate]
If you are creating new array then try this : $arr = [‘key’ => ‘value’]; And if array is already created then try this : $arr[‘key’] = ‘value’;
How to create a Multidimensional ArrayList in Java?
ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>(); Depending on your requirements, you might use a Generic class like the one below to make access easier: import java.util.ArrayList; class TwoDimentionalArrayList<T> extends ArrayList<ArrayList<T>> { public void addToInnerArray(int index, T element) { while (index >= this.size()) { this.add(new ArrayList<T>()); } this.get(index).add(element); } public void addToInnerArray(int index, int index2, T element) … Read more
Transforming a row vector into a column vector in Numpy
you can use the transpose operation to do this: Example: In [2]: a = np.array([[1,2], [3,4], [5,6]]) In [5]: a.shape Out[5]: (3, 2) In [6]: a_trans = a.T #or: np.transpose(a), a.transpose() In [8]: a_trans.shape Out[8]: (2, 3) In [7]: a_trans Out[7]: array([[1, 3, 5], [2, 4, 6]]) Note that the original array a will still … Read more
2D array values C++
Like this: int main() { int arr[2][5] = { {1,8,12,20,25}, {5,9,13,24,26} }; } This should be covered by your C++ textbook: which one are you using? Anyway, better, consider using std::vector or some ready-made matrix class e.g. from Boost.