multidimensional-array

How to make dynamic multi-dimensional array in ruby?

守給你的承諾、 提交于 2020-01-01 10:17:27
问题 I have a beginner ruby question about multi dimensional arrays. I want to sort entries by year and month. So I want to create a multi-dimensional array that would contain years -> months -> entries of month So the array would be like: 2009 -> 08 -> Entry 1 -> Entry 2 09 -> Entry 3 2007 -> 10 -> Entry 5 Now I have: @years = [] @entries.each do |entry| timeobj = Time.parse(entry.created_at.to_s) year = timeobj.strftime("%Y").to_i month = timeobj.strftime("%m").to_i tmparr = [] tmparr << {month=

board game win situation - searching algorithm

旧巷老猫 提交于 2020-01-01 09:28:30
问题 I'm looking for possibly efficient algorithm to detect "win" situation in a gomoku (five-in-a-row) game, played on a 19x19 board. Win situation happens when one of the players manages to get five and NO MORE than five "stones" in a row (horizontal, diagonal or vertical). I have the following data easily accessible: previous moves ("stones") of both players stored in a 2d array (can be also json notation object), with variables "B" and "W" to difference players from each other, "coordinates"

Reverse an arbitrary dimension in an ndarray

拈花ヽ惹草 提交于 2020-01-01 08:54:26
问题 I'm working with an n-dimensional array, and I'd like a way to reverse a numbered dimension. So rather than rev = a[:,:,::-1] I'd like to be able to write rev = a.reverse(dimension=2) or something similar. I can't seem to find examples that don't rely on the former syntax. 回答1: For anyone coming across this in the future: Numpy 1.12+ has the function np.flip(array, dimension), which does exactly as requested. Even better, it returns a view of the data rather than a copy, and so it happens in

Reverse an arbitrary dimension in an ndarray

我的未来我决定 提交于 2020-01-01 08:54:02
问题 I'm working with an n-dimensional array, and I'd like a way to reverse a numbered dimension. So rather than rev = a[:,:,::-1] I'd like to be able to write rev = a.reverse(dimension=2) or something similar. I can't seem to find examples that don't rely on the former syntax. 回答1: For anyone coming across this in the future: Numpy 1.12+ has the function np.flip(array, dimension), which does exactly as requested. Even better, it returns a view of the data rather than a copy, and so it happens in

How to save an NxNxN array (or Matrix) into a file in Julia (or Python)?

丶灬走出姿态 提交于 2020-01-01 08:34:47
问题 I'm working on a Jupyter notebook and currently using Julia I'm trying to save a 3x3x3 Array into a textfile so when I include it in another notebook, the array is a 3x3x3 Array too. Any suggestions? Thanks in advance. 回答1: You could use the JLD.jl (Julia Data) package: Pkg.add("JLD") using JLD r = rand(3, 3, 3) save("data.jld", "data", r) load("data.jld")["data"] The advantage of the JLD package is that it preserves the exact type information of each variable. 回答2: Okay I admit that I am a

Algorithm to find peaks in 2D array

自古美人都是妖i 提交于 2020-01-01 08:25:48
问题 Let's say I have a 2D accumulator array in java int[][] array . The array could look like this: (x and z axes represent indexes in the array, y axis represents values - these are images of an int[56][56] with values from 0 ~ 4500) or What I need to do is find peaks in the array - there are 2 peaks in the first one and 8 peaks in the second array. These peaks are always 'obvious' (there's always a gap between peaks), but they don't have to be similar like on these images, they can be more or

Multidimensional Vectors in C++

一曲冷凌霜 提交于 2020-01-01 07:23:09
问题 I just started learning C++. I was trying to grasp the syntax for multidimensional arrays and vectors when I started to get fairly confused. I get how to initialize multidimensional arrays. It seems straightforward: Rows followed by columns. However, vectors are a little more challenging. Do I have to initialize them in the same way or do I create a vector of vectors. Somebody please help. 回答1: If you are able to use C++11, multidimensional arrays and vectors of vectors can be initialized in

Merge php array based on two unique values

故事扮演 提交于 2020-01-01 07:18:45
问题 I'm looking for a way to do a pretty odd array merge between multidimensional arrays. Take the following two arrays arrayOne and arrayTwo as examples. I'd like to merge the arrays into arrayThree , which will show arrays items that are unique if both number and letter combined are unique. It'll merge the values from one array with another and if the value isn't present, then it'll provide an empty string. (see arrayThree for what I mean) Any ideas? $arrayOne = array( array('number' => 1,

np.ndarray with Periodic Boundary conditions

亡梦爱人 提交于 2020-01-01 05:45:10
问题 Problem To impose np.ndarray periodic boundary conditions as laid out below Details Wrap the indexing of a python np.ndarray around the boundaries in n -dimensions This is a periodic boundary condition forming an n -dimensional torus Wrapping only occurs in the case that the value returned is scalar (a single point). Slices will be treated as normal and will not be wrapped An example and counterexample are given below: a = np.arange(27).reshape(3,3,3) b = Periodic_Lattice(a) # A theoretical

How to find the index of an item in a multidimensional array swiftily?

六月ゝ 毕业季﹏ 提交于 2020-01-01 02:41:25
问题 Let's say I have this array: let a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Now I want something like this: public func indicesOf(x: Int, array: [[Int]]) -> (Int, Int) { ... } so that I can call it like this: indicesOf(7, array: a) // returns (2, 0) Of course, I can use: for i in 0..<array.count { for j in 0..<array[i].count { if array[i][j] == x { return (i, j) } } } But that is not even close to swifty! I want a way to do this which is swifty. I think maybe I can use reduce or map ? 回答1: You can