vectorization

Finding index by iterating over each row of matrix

房东的猫 提交于 2019-12-20 04:19:36
问题 I have an numpy array 'A' of size 5000x10 . I also have another number 'Num' . I want to apply the following to each row of A: import numpy as np np.max(np.where(Num > A[0,:])) Is there a pythonic way than writing a for loop for above. 回答1: You could use argmax - A.shape[1] - 1 - (Num > A)[:,::-1].argmax(1) Alternatively with cumsum and argmax - (Num > A).cumsum(1).argmax(1) Explanation : With np.max(np.where(..) , we are basically looking to get the last occurrence of matches along each row

Creation of a specific vector without loop or recursion in R

怎甘沉沦 提交于 2019-12-20 04:11:47
问题 I've got a first vector, let's say x that consists only of 1's and -1's. Then, I have a second vector y that consists of 1's, -1's, and zeros. Now, I'd like to create a vector z that contains in index i a 1 if x[i] equals 1 and a 1 exists within the vector y between the n precedent elements (y[(i-n):i]) ... more formally: z <- ifelse(x == 1 && 1 %in% y[(index(y)-n):index(y)],1,0) I'm looking to create such a vector in R without looping or recursion. The proposition above does not work since

Numpy element-wise in operation

*爱你&永不变心* 提交于 2019-12-20 04:07:44
问题 Suppose I have a column vector y with length n, and I have a matrix X of size n*m. I want to check for each element i in y, whether the element is in the corresponding row in X. What is the most efficient way of doing this? For example: y = [1,2,3,4].T and X =[[1, 2, 3],[3, 4, 5],[4, 3, 2],[2, 2, 2]] Then the output should be [1, 0, 1, 0] or [True, False, True, False] which ever is easier. Of course we can use a for loop to iterate through both y and X, but is there any more efficient way of

SIMD vectorize atan2 using ARM NEON assembly

百般思念 提交于 2019-12-20 01:12:34
问题 I want to calculate the magnitude and the angle of 4 points using neon instructions SIMD and arm assembly. There is a built in library in most languages, C++ in my case, which calculates the angle (atan2) but for only one pair of floating point variables (x and y). I would like to exploit SIMD instructions that deal with q registers in order to calculate atan2 for a vector of 4 values. The accuracy is required not to be high, the speed is more important. I already have a few assembly

Subsetting lists via logical index vectors

最后都变了- 提交于 2019-12-19 23:23:21
问题 I have a complex list and need to select a subset from it, based on the value of a boolean element (I need records with hidden value equal to FALSE ). I've tried the following code, based on index vectors , but it fails (as shown at the end of this output): startups <- data$startups[data$startups$hidden == FALSE] Or, alternatively: startups <- data$startups[!as.logical(data$startups$hidden)] Interactive R session proves that the data is there: Browse[1]> str(data$startups, list.len=3) List of

Vectorize haversine distance computation along path given by list of coordinates

China☆狼群 提交于 2019-12-19 22:12:33
问题 I have a list of coordinates and can calculate a distance matrix among all points using the haversine distance metric. Coordinates come a as numpy.array of shape (n, 2) of (latitude, longitude) pairs: [[ 16.34576887 -107.90942116] [ 12.49474931 -107.76030036] [ 27.79461514 -107.98607881] ... [ 12.90258404 -107.96786569] [ -6.29109889 -107.88681145] [ -2.68531605 -107.72796034]] I can also extract the distance along the path implied by the sequence of coordinates like so: coordinates = np

Vectorize haversine distance computation along path given by list of coordinates

十年热恋 提交于 2019-12-19 22:12:12
问题 I have a list of coordinates and can calculate a distance matrix among all points using the haversine distance metric. Coordinates come a as numpy.array of shape (n, 2) of (latitude, longitude) pairs: [[ 16.34576887 -107.90942116] [ 12.49474931 -107.76030036] [ 27.79461514 -107.98607881] ... [ 12.90258404 -107.96786569] [ -6.29109889 -107.88681145] [ -2.68531605 -107.72796034]] I can also extract the distance along the path implied by the sequence of coordinates like so: coordinates = np

Create matrix by repeatedly overlapping a vector

三世轮回 提交于 2019-12-19 21:42:55
问题 I'm having great difficulty coding the following in MATLAB: Suppose you have the following vector: a b c d e f g h ... Specifying an (even) window size, create the following matrix of dimensions L rows by n columns (example, L = 4 ): a c e ... b d f ... c e g ... d f h ... Even more difficult is taking a vector of arbitrary length, specifying the number of windows, and optimizing (maximizing) the window size so less values at the end of the vector are dumped. 回答1: Create the matrix of indices

matlab. vectorization within if/else if/else statements

依然范特西╮ 提交于 2019-12-19 17:45:13
问题 I need some help with the following code: if x(:,3)>x(:,4) output=[x(:,1)-x(:,2)]; elseif x(:,3)<x(:,4) output=[x(:,2)-x(:,1)]; else output=NaN end Here is a sample data: matrix x output 10 5 1 2 -5 10 5 2 1 5 NaN 1 1 3 NaN I'm not sure how to make the code work. It just takes the first argument and ignores the else if and else arguments. Please help. Thank you. 回答1: if x(:,3)>x(:,4) doesn't really work, if expects either true or false not a vector. So it only evaluates the first element of

Is it possible to numpy.vectorize an instance method?

别来无恙 提交于 2019-12-19 14:05:15
问题 I've found that the numpy.vectorize allows one to convert 'ordinary' functions which expect a single number as input to a function which can also convert a list of inputs into a list in which the function has been mapped to each input. For example, the following tests pass: import numpy as np import pytest @np.vectorize def f(x): if x == 0: return 1 else: return 2 def test_1(): assert list(f([0, 1, 2])) == [1, 2, 2] def test_2(): assert f(0) == 1 if __name__ == "__main__": pytest.main([__file