vectorization

kdb/q: How to apply a string manipulation function to a vector of strings to output a vector of strings?

不打扰是莪最后的温柔 提交于 2021-02-05 08:18:51
问题 Thanks in advance for the help. I am new to kdb/q, coming from a Python and C++ background. Just a simple syntax question: I have a string with fields and their corresponding values pp_str: "field_1:abc field_2:xyz field_3:kdb" I wrote an atomic (scalar) function to extract the value of a given field. get_field_value: {[field; pp_str] pp_fields: " " vs pp_str; pid_field: pp_fields[where like[pp_fields; field,":*"]]; start_i: (pid_field[0] ss ":")[0] + 1; end_i: count pid_field[0]; indices:

kdb/q: How to apply a string manipulation function to a vector of strings to output a vector of strings?

天大地大妈咪最大 提交于 2021-02-05 08:18:10
问题 Thanks in advance for the help. I am new to kdb/q, coming from a Python and C++ background. Just a simple syntax question: I have a string with fields and their corresponding values pp_str: "field_1:abc field_2:xyz field_3:kdb" I wrote an atomic (scalar) function to extract the value of a given field. get_field_value: {[field; pp_str] pp_fields: " " vs pp_str; pid_field: pp_fields[where like[pp_fields; field,":*"]]; start_i: (pid_field[0] ss ":")[0] + 1; end_i: count pid_field[0]; indices:

squash all consecutive ones in a python list

自古美人都是妖i 提交于 2021-02-05 07:24:06
问题 I have variable lists filled with 0s and 1s , like: l1 = [1,1,1,0,1,1] l2 = [0,1,1,0,1,1,0,0,1] what is the most efficient way to create new lists that squash all consecutive 1s . So the result would be: l1_new = [1,0,1] l2_new = [0,1,0,1,0,0,1] Hint: numpy/vectorization or some logical operation would be great! 回答1: Here's one approach using with np.diff and bitwise operations : l1 = [1,1,1,0,1,1] l2 = [0,1,1,0,1,1,0,0,1] a = np.array(l2) a[~((np.diff(a,prepend=False)==0) & (a==1))] # array(

A broadcasting issue involving where to put the padding

时光毁灭记忆、已成空白 提交于 2021-02-04 15:41:09
问题 Introduction I have a function func which is vectorizable, and I vectorize it using np.frompyfunc . Rather than using a nested for loop, I want to call it only once, and thus I'll need to pad the inputs with np.newaxis 's. My goal is to get rid of the two nested for loops and use the numpy.array broadcasting feature instead. Here is the MWE for loops (I want to get rid of the for loops and instead pad the variables c_0 , c_1 , rn_1 , rn_2 , and factor when calling myfunc . MWE of the problem

Numpy-vectorized function to repeat blocks of consecutive elements

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-04 07:34:49
问题 Numpy has а repeat function, that repeats each element of the array a given (per element) number of times. I want to implement a function that does similar thing but repeats not individual elements, but variably sized blocks of consecutive elements. Essentially I want the following function: import numpy as np def repeat_blocks(a, sizes, repeats): b = [] start = 0 for i, size in enumerate(sizes): end = start + size b.extend([a[start:end]] * repeats[i]) start = end return np.concatenate(b) For

R ~ Vectorization of a user defined function

混江龙づ霸主 提交于 2021-02-02 09:23:45
问题 I need to write a function that will count the number of working days (minus weekends, and a vector of other local bank holidays), but the problem I'm coming up against is more simply illustrated with just counting the number of weekdays. Here is a function that will give the number of weekdays between two dates: removeWeekends <- function(end, start){ range <- as.Date(start:end, "1970-01-01") range<- range[sapply(range, function(x){ if(!chron::is.weekend(x)){ return(TRUE) }else{ return(FALSE

Vectorize function evaluation in MATLAB

跟風遠走 提交于 2021-01-29 12:50:26
问题 I have the following function, function Vectorize() a = randn(1,5) b = randn(1,5) c = zeros(1,5) for i=1:5 c(i) = (a(i) - b(i))/(1+a(i)/2+b(i)/3) end I want to vectorize the above function evaluation and replace the for loop. I could do c = a -b , that finds the difference between two row vectors.I am not sure how to handle the division a/2 and b/2. Could someone help? 回答1: You need the element wise division operation ./ c = (a - b)./(1+a/2+b/3) If you divide a vector by a scalar, this is not

Mean of non-zero elements in 3d array

半腔热情 提交于 2021-01-29 11:34:41
问题 I have this i x j x k 3d matrix (it's a movie). Without loops, I'm trying to take the mean of the non-zero positive elements in each ixj array and put these values into a 1x1xk matrix. I've been searching for quite a while now, and although there's plenty of solutions to accomplish this for a 2d matrix, I can't for the life of me find a way to do it for a 3d matrix without using a loop. 回答1: What if you convert each image (frame) into an array: % Remove negative and zero elements A(A<=0) = 0;

Using Apply or Vectorize to apply custom function to a dataframe

左心房为你撑大大i 提交于 2021-01-29 09:14:26
问题 I am attempting to apply a custom function that calls components of that dataframe to do a calculation. I have made a trivial example below because my actual problem is very hard to make a reproducible example. In the below example I want to have the first two columns be added together to create a third column which is the sum of them. Below is an example I found online that gets close to what I want: celebrities=data.frame(name=c("Andrew","matt","Dany","Philip","John","bing","Monica"), age=c

NumPy - Vectorizing bincount over 2D array column wise with weights

江枫思渺然 提交于 2021-01-29 04:31:56
问题 I've been looking at the solutions here and here but failing to see how I can apply it to my structures. I have 3 arrays: an (M, N) of zeros, and (P,) of indexes (some repeat) and an (P, N) of values. I can accomplish it with a loop: # a: (M, N) # b: (P, N) # ix: (M,) for i in range(N): a[:, i] += np.bincount(ix, weights=b[:, i], minlength=M) I've not seen any examples that use indexes in this manner, or with the weights keyword. I understand I need to bring everything into a 1D array to