multidimensional-array

PHP Multidimensional Array - Search for value and get the sub-array

家住魔仙堡 提交于 2019-12-29 08:32:47
问题 Given an array like $clusters = array( "clustera" => array( '101', '102', '103', '104' ), "clusterb" => array( '201', '202', '203', '204' ), "clusterc" => array( '301', '302', '303', '304' ) ); How can I search for a server (e.g. 202) and get back it's cluster? i.e. search for 202 and the response is "clusterb" I tried using array_search but it seems that is only for monodimensional arrays right? (i.e. complains that second argument is wrong dataype if I give it $clusters) Many Thanks! 回答1:

PHP Multidimensional Array - Search for value and get the sub-array

☆樱花仙子☆ 提交于 2019-12-29 08:32:08
问题 Given an array like $clusters = array( "clustera" => array( '101', '102', '103', '104' ), "clusterb" => array( '201', '202', '203', '204' ), "clusterc" => array( '301', '302', '303', '304' ) ); How can I search for a server (e.g. 202) and get back it's cluster? i.e. search for 202 and the response is "clusterb" I tried using array_search but it seems that is only for monodimensional arrays right? (i.e. complains that second argument is wrong dataype if I give it $clusters) Many Thanks! 回答1:

Create an array where each element stores its indices

旧时模样 提交于 2019-12-29 07:53:52
问题 I want to create a 2d numpy array where every element is a tuple of its indices. Example (4x5): array([[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4]], [[1, 0], [1, 1], [1, 2], [1, 3], [1, 4]], [[2, 0], [2, 1], [2, 2], [2, 3], [2, 4]], [[3, 0], [3, 1], [3, 2], [3, 3], [3, 4]]]) I would create an python list with the following list comprehension: [[(y,x) for x in range(width)] for y in range(height)] Is there a faster way to achieve the same, maybe with numpy methods? 回答1: Here's an initialization

Javascript: sort multidimensional array

牧云@^-^@ 提交于 2019-12-29 04:30:15
问题 After creating a multi-dim array like this, how do I sort it? Assuming 'markers' is already defined: var location = []; for (var i = 0; i < markers.length; i++) { location[i] = {}; location[i]["distance"] = "5"; location[i]["name"] = "foo"; location[i]["detail"] = "something"; } For the above example, I need to sort it by 'distance'. I've seen other questions on sorting arrays and multi-dim arrays, but none seem to work for this. 回答1: location.sort(function(a,b) { // assuming distance is

How do I allocate memory and copy 2D arrays between CPU / GPU in CUDA without flattening them?

▼魔方 西西 提交于 2019-12-29 02:00:12
问题 So I want to allocate 2D arrays and also copy them between the CPU and GPU in CUDA, but I am a total beginner and other online materials are very difficult for me to understand or are incomplete. It is important that I am able to access them as a 2D array in the kernel code as shown below. Note that height != width for the arrays, that's something that further confuses me if it's possible as I always struggle choosing grid size. I've considered flattening them, but I really want to get it

Why is B = numpy.dot(A,x) so much slower looping through doing B[i,:,:] = numpy.dot(A[i,:,:],x) )?

久未见 提交于 2019-12-29 01:34:35
问题 I'm getting some efficiency test results that I can't explain. I want to assemble a matrix B whose i-th entries B[i,:,:] = A[i,:,:].dot(x), where each A[i,:,:] is a 2D matrix, and so is x. I can do this three ways, to test performance I make random ( numpy.random.randn ) matrices A = (10,1000,1000), x = (1000,1200). I get the following time results: (1) single multidimensional dot product B = A.dot(x) total time: 102.361 s (2) looping through i and performing 2D dot products # initialize B =

Generate a two dimensional array via LINQ

人走茶凉 提交于 2019-12-28 20:03:53
问题 I am trying to create a matrix of doubles, representing a correlation between entities. Here's how I'm doing it via LINQ double[][] correlationsRaw = (from e in entitiesInOrder select (from f in entitiesInOrder select correlations.GetCorrelation(e, f) ).ToArray()).ToArray(); That works fine. But what I want is a two dimensional array ( double[,] ), not a jagged array. Obviously, I can write some nested for loop to convert one into the other. But is there some elegant LINQ trick I can use here

Generate a two dimensional array via LINQ

一世执手 提交于 2019-12-28 20:01:27
问题 I am trying to create a matrix of doubles, representing a correlation between entities. Here's how I'm doing it via LINQ double[][] correlationsRaw = (from e in entitiesInOrder select (from f in entitiesInOrder select correlations.GetCorrelation(e, f) ).ToArray()).ToArray(); That works fine. But what I want is a two dimensional array ( double[,] ), not a jagged array. Obviously, I can write some nested for loop to convert one into the other. But is there some elegant LINQ trick I can use here

Dynamic memory allocation for 3D array [duplicate]

烂漫一生 提交于 2019-12-28 16:02:58
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Malloc a 3-Dimensional array in C? dynamic allocation/deallocation of 2D & 3D arrays How can i allocate 3D arrays using malloc? 回答1: array = malloc(num_elem * num_elem * num_elem * sizeof(array_elem)); Why not? :) 回答2: There are two different ways to allocate a 3D array. You can allocate it either as a 1D array of pointers to a (1D array of pointers to a 1D array). This can be done as follows: int dim1, dim2,

Implementing goMongoDB-like Query expression object evaluation

十年热恋 提交于 2019-12-28 13:21:55
问题 I've been looking for a MongoDb-like ( http://docs.mongodb.org/manual/applications/read/#find, docs.mongodb.org/manual/reference/operators/ ) query expression object evaluation function implementation or a class. It may cover not all the advanced features, and should have extensible architecture. MongoDB-like query expression objects are easy for understanding and usage , providing ability to write clean, self-explaining code, because both query and objects to search in, are associative