indices

Assigning identical array indices at once in Python/Numpy

∥☆過路亽.° 提交于 2019-12-18 13:26:24
问题 I want to find a fast way (without for loop) in Python to assign reoccuring indices of an array. This is the desired result using a for loop: import numpy as np a=np.arange(9, dtype=np.float64).reshape((3,3)) # The array indices: [2,3,4] are identical. Px = np.uint64(np.array([0,1,1,1,2])) Py = np.uint64(np.array([0,0,0,0,0])) # The array to be added at the array indices (may also contain random numbers). x = np.array([.1,.1,.1,.1,.1]) for m in np.arange(len(x)): a[Px[m]][Py[m]] += x print a

Using list elements and indices together

徘徊边缘 提交于 2019-12-18 12:22:33
问题 I've always found it awkward to have a function or expression that requires use of the values, as well as indices, of a list (or array, applies just the same) in Haskell. I wrote validQueens below while experimenting with the N-queens problem here ... validQueens x = and [abs (x!!i - x!!j) /= j-i | i<-[0..length x - 2], j<-[i+1..length x - 1]] I didn't care for the use of indexing, all the plus and minuses, etc. It feels sloppy. I came up with the following: enumerate x = zip [0..length x - 1

Python equivalent of which() in R

孤者浪人 提交于 2019-12-18 11:19:11
问题 I am trying to take the following R statement and convert it to Python using NumPy: 1 + apply(tmp,1,function(x) length(which(x[1:k] < x[k+1]))) Is there a Python equivalent to which()? Here, x is row in matrix tmp , and k corresponds to the number of columns in another matrix. Previously, I tried the following Python code, and received a Value Error (operands could not be broadcast together with shapes): for row in tmp: print np.where(tmp[tmp[:,range(k)] < tmp[:,k]]) 回答1: The Python code

compare two lists in python and return indices of matched values

夙愿已清 提交于 2019-12-17 19:35:04
问题 For two lists a and b, how can I get the indices of values that appear in both? For example, a = [1, 2, 3, 4, 5] b = [9, 7, 6, 5, 1, 0] return_indices_of_a(a, b) would return [0,4] , with (a[0],a[4]) = (1,5) . 回答1: The best way to do this would be to make b a set since you are only checking for membership inside it. >>> a = [1, 2, 3, 4, 5] >>> b = set([9, 7, 6, 5, 1, 0]) >>> [i for i, item in enumerate(a) if item in b] [0, 4] 回答2: def return_indices_of_a(a, b): b_set = set(b) return [i for i,

Can someone please explain the “indices trick”?

我只是一个虾纸丫 提交于 2019-12-17 09:43:48
问题 I noticed the "indices trick" being mentioned in the context of pretty-printing tuples. It sounded interesting, so I followed the link. Well, that did not go well. I understood the question, but could really not follow what was going on. Why do we even need indices of anything? How do the different functions defined there help us? What is 'Bare'? etc. Can someone give a play-by-play of that thing for the less-than-experts on parameter packs and variadic tuples? 回答1: The problem is: we have a

Error with square root function and powers in Matlab R2014b [closed]

此生再无相见时 提交于 2019-12-14 03:22:21
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I am carrying out a series of calculations using the sqrt() function. My colleagues and I have noticed that we get different results when using the same inputs. Has anyone encountered this problem before? This is an example: input1 = 4; input2 = 8; result = sqrt(input1^2 + input2^2) Result then

Retrieving largest indices of the bounded element in a multidimensional array in Scala

跟風遠走 提交于 2019-12-13 13:16:54
问题 I have an multidimensional Array: val M = Array.ofDim[Int](V, N) Goal is to find largest V dimension index for which there exists a bounded element 0 < w0 <= W and return both indices and element value. Currently I have this code snippet which works, but wondering if there is a nicer, more efficient way to do this. M.zipWithIndex.reverse.collectFirst({ case (arr, ind) if arr.exists(a => a <= W && a > 0) => { arr.zipWithIndex.find(a => a._1 <= W && a._1 > 0) match { case Some((weight, ind2)) =

Python: TypeError: list indices must be integers, not list

◇◆丶佛笑我妖孽 提交于 2019-12-12 21:06:27
问题 I've got two lists of coordinates, they look like this: list_kp2_ok: [[1185.60009765625, 933.6000366210938], [1310.4000244140625, 828.0000610351562], [1067.0, 979.0], [1310.0, 828.0], [1423.2000732421875, 814.800048828125], [1306.0, 828.0], [3634.0, 605.0], [1308.0960693359375, 827.7120971679688], [1422.7200927734375, 815.0400390625], [1185.1199951171875, 933.1200561523438], [1186.56005859375, 923.0400390625], [1306.3681640625, 829.4401245117188], [1194.393798828125, 839.80810546875], [1187

Best way to create Image from palette Color array + indice byte array?

孤人 提交于 2019-12-12 05:22:17
问题 I'm developing a Java component for displaying some videos and for each frame of the video, my decoder gives me a Color[256] palette + a width*height bytes pixel indices array. Here's how I create my BufferedImage right now: byte[] iArray = new byte[width * height * 3]; int j = 0; for (byte i : this.lastFrameData) { iArray[j] = (byte) this.currentPalette[i & 0xFF].getRed(); iArray[j + 1] = (byte) this.currentPalette[i & 0xFF].getGreen(); iArray[j + 2] = (byte) this.currentPalette[i & 0xFF]

Python keeping track of indices in lists at certain points

蓝咒 提交于 2019-12-12 04:59:38
问题 I'm having some trouble with iteration and keeping track of various indices and values at different points in my list (I'm new to Python). I am running a series of cycles, but want to determine their starts and end times. Experiments starts at around 0 and end at around 50. Here is what a list of cycles look like: c = [0, 10, 11, 48, 50.5, 0.48, 17, 18, 23, 29, 33, 34.67, 50.1, 0.09, 7, 41, 45, 50] Here is an example of what the output should look like: C 1: Start: (0, 0) # starts at index 0,