multidimensional-array

How to find the index of a value in 2d array in Python?

别等时光非礼了梦想. 提交于 2019-12-28 11:54:35
问题 I need to figure out how I can find all the index of a value in a 2d numpy array. For example, I have the following 2d array: ([[1 1 0 0], [0 0 1 1], [0 0 0 0]]) I need to find the index of all the 1's and 0's. 1: [(0, 0), (0, 1), (1, 2), (1, 3)] 0: [(0, 2), (0, 3), (1, 0), (1, 1), (the entire all row)] I tried this but it doesn't give me all the indexes: t = [(index, row.index(1)) for index, row in enumerate(x) if 1 in row] Basically, it gives me only one of the index in each row [(0, 0), (1

How do I find the size of a 2D array? [duplicate]

做~自己de王妃 提交于 2019-12-28 11:43:45
问题 This question already has answers here : How do you get the width and height of a multi-dimensional array? (5 answers) Closed last year . If I declare this array... string[,] a = { {"0", "1", "2"}, {"0", "1", "2"}, {"0", "1", "2"}, {"0", "1", "2"}, }; Then I can measure the length with a.Length which is 12. How do I measure the dimension of the arrays within? If I try... a[0].Length I get Wrong number of indices inside []; expected 2 . What gives? 回答1: You want the GetLength() method of your

How do I find the size of a 2D array? [duplicate]

百般思念 提交于 2019-12-28 11:42:26
问题 This question already has answers here : How do you get the width and height of a multi-dimensional array? (5 answers) Closed last year . If I declare this array... string[,] a = { {"0", "1", "2"}, {"0", "1", "2"}, {"0", "1", "2"}, {"0", "1", "2"}, }; Then I can measure the length with a.Length which is 12. How do I measure the dimension of the arrays within? If I try... a[0].Length I get Wrong number of indices inside []; expected 2 . What gives? 回答1: You want the GetLength() method of your

Group subarrays by one column, make comma-separated values from other column within groups

旧城冷巷雨未停 提交于 2019-12-28 07:17:04
问题 I have a array that looks like this: $array = [ ["444", "0081"], ["449", "0081"], ["451", "0081"], ["455", "2100"], ["469", "2100"] ]; I need to group as a new array that looks like: array ( 0 => array ( 0 => '444,449,451', 1 => '0081', ), 1 => array ( 0 => '455,469', 1 => '2100', ), ) I'd tried many scripts, but with no success. function _group_by($array, $key) { $return = array(); foreach($array as $val) { $return[$val[$key]][] = $val; } return $return; } $newArray = _group_by($array, 1); /

Group subarrays by one column, make comma-separated values from other column within groups

ε祈祈猫儿з 提交于 2019-12-28 07:16:10
问题 I have a array that looks like this: $array = [ ["444", "0081"], ["449", "0081"], ["451", "0081"], ["455", "2100"], ["469", "2100"] ]; I need to group as a new array that looks like: array ( 0 => array ( 0 => '444,449,451', 1 => '0081', ), 1 => array ( 0 => '455,469', 1 => '2100', ), ) I'd tried many scripts, but with no success. function _group_by($array, $key) { $return = array(); foreach($array as $val) { $return[$val[$key]][] = $val; } return $return; } $newArray = _group_by($array, 1); /

Efficiently multiply elements of each row together

岁酱吖の 提交于 2019-12-28 07:04:21
问题 Given a ndarray of size (n, 3) with n around 1000, how to multiply together all elements for each row, fast? The (inelegant) second solution below runs in about 0.3 millisecond, can it be improved? # dummy data n = 999 a = np.random.uniform(low=0, high=10, size=n).reshape(n/3,3) # two solutions def prod1(array): return [np.prod(row) for row in array] def prod2(array): return [row[0]*row[1]*row[2] for row in array] # benchmark start = time.time() prod1(a) print time.time() - start # 0.0015

how to join two multidimensional arrays in php

孤街浪徒 提交于 2019-12-28 06:48:13
问题 how to join two multidimensional arrays in php? I have two multidimensional arrays A and B. I need to join A and B to form a new array C as follows $A = array( array("a1"=>1,"b1"=>2,"c1"=>"A"), array("a1"=>1,"b1"=>16,"c1"=>"Z"), array("a1"=>3,"b1"=>8,"c1"=>"A")); $B = array( array("a2"=>1,"b2"=>2,"b2"=>"A"), array("a2"=>1,"b2"=>16,"b2"=>"G"), array("a2"=>3,"b2"=>8,"b2"=>"A")); //join A and B to form C $C=array( array("a1"=>1,"b1"=>2,"c1"=>"A"), array("a1"=>1,"b1"=>16,"c1"=>"Z"), array("a1"=>3

Select along one of n dimensions in array

て烟熏妆下的殇ゞ 提交于 2019-12-28 05:54:11
问题 I have an array in R, created by a function like this: A <- array(data=NA, dim=c(2,4,4), dimnames=list(c("x","y"),NULL,NULL)) And I would like to select along one dimension, so for the example above I would have: A["x",,] dim(A["x",,]) #[1] 4 4 Is there a way to generalize if I do not know in advance how many dimensions (in addition to the named one I want to select by) my array might have? I would like to write a function that takes input that might formatted as A above, or as: B <- c(1,2)

Select along one of n dimensions in array

ぐ巨炮叔叔 提交于 2019-12-28 05:53:48
问题 I have an array in R, created by a function like this: A <- array(data=NA, dim=c(2,4,4), dimnames=list(c("x","y"),NULL,NULL)) And I would like to select along one dimension, so for the example above I would have: A["x",,] dim(A["x",,]) #[1] 4 4 Is there a way to generalize if I do not know in advance how many dimensions (in addition to the named one I want to select by) my array might have? I would like to write a function that takes input that might formatted as A above, or as: B <- c(1,2)

push() a two-dimensional array

有些话、适合烂在心里 提交于 2019-12-28 05:49:05
问题 I'm trying to push to a two-dimensional array without it messing up, currently My array is: var myArray = [ [1,1,1,1,1], [1,1,1,1,1], [1,1,1,1,1] ] And my code I'm trying is: var r = 3; //start from rows 3 var c = 5; //start from col 5 var rows = 8; var cols = 7; for (var i = r; i < rows; i++) { for (var j = c; j < cols; j++) { myArray[i][j].push(0); } } That should result in the following: var myArray = [ [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0