vectorization

Applying a function on array that returns outputs with different size in a vectorized manner

 ̄綄美尐妖づ 提交于 2020-01-19 13:10:08
问题 How to apply a function that returns non scalar output to arrays using arrayfun ? For example - How to vectorize the following code? array = magic(5); A = cell(size(array)); for i=1:5 for j=1:5 A{i,j} = 1:array(i,j); end end This naive attempt to vectorize does not work, because the output is not a scalar array = magic(5); result = arrayfun(@(x)(1:x),array); 回答1: There are 2 methods to achieve it: It is possible to set 'UniformOutput' to false. Then, the result is a cell array. result =

Applying a function on array that returns outputs with different size in a vectorized manner

点点圈 提交于 2020-01-19 13:09:38
问题 How to apply a function that returns non scalar output to arrays using arrayfun ? For example - How to vectorize the following code? array = magic(5); A = cell(size(array)); for i=1:5 for j=1:5 A{i,j} = 1:array(i,j); end end This naive attempt to vectorize does not work, because the output is not a scalar array = magic(5); result = arrayfun(@(x)(1:x),array); 回答1: There are 2 methods to achieve it: It is possible to set 'UniformOutput' to false. Then, the result is a cell array. result =

Auto-vectorization of loop containing comparisons

不羁的心 提交于 2020-01-17 07:43:09
问题 I'm trying to use the Visual C++ 2013 auto-vectorizer to make the following loop vectorized ( /arch:AVX2 ) but the compiler refuses and gives the following message: info C5002: loop not vectorized due to reason '1100' This reason code means Loop contains control flow—for example, "if" or "?". I have tried to split the comparisons and the final assignment into a separate loop but that seems inefficient when there are intrinsics available for performing comparisons on floating point values. Why

Assigning a Matlab vector according to a function

懵懂的女人 提交于 2020-01-17 04:15:29
问题 I basically want to vectorize the following: vect_y = zeros(1,numel(vect_x); for i = 1:numel(vect_x) vect_y = sum(vect_x(1:i)); end Is this possible? As an example, I was trying to use arrayfun the following way: y = arrayfun(@(y) sum(y), vect_x(1:1), vect_x(1:2), ..., vect_x(1:n)); But this won't work and it's not clean. edit: So I know now that cumsum solves the above, but I am curious as to how I would do this for any function. 回答1: What you want can be done with the cumsum function

Scikit Learn and Count Vectorizer Error

拈花ヽ惹草 提交于 2020-01-16 18:51:48
问题 Does anyone know where this ImportError is originating from and how to fix it? I'm working from a CSV file to do some text mining. At this point, I'm simply trying to tokenize the words in some job descriptions in the file and then vectorize and count the dimensions. However, I am getting this error. The original code follows this error message for you to see. I've tried uninstalling Anaconda and reinstalling it as well as all of the packages. This code runs absolutely fine on my PC (an old

Parallelize or vectorize all-against-all operation on a large number of matrices?

放肆的年华 提交于 2020-01-16 04:08:12
问题 I have approximately 5,000 matrices with the same number of rows and varying numbers of columns (20 x ~200). Each of these matrices must be compared against every other in a dynamic programming algorithm. In this question, I asked how to perform the comparison quickly and was given an excellent answer involving a 2D convolution. Serially, iteratively applying that method, like so list = who('data_matrix_prefix*') H = cell(numel(list),numel(list)); for i=1:numel(list) for j=1:numel(list) if i

Vectorizing merge/union of two sorted arrays

半城伤御伤魂 提交于 2020-01-15 08:43:45
问题 I have recently started looking into opportunities to speed up my code by using vector instructions. My code heavily relies on operations with sets - for simplicity let us assume that these are represented as sorted arrays of 16bit unsigned integers. The operations I need to perform are: Intersection (i.e., each element contained in both sets is to be present in the output set) Union (i.e., each element that is contained in at least one of the sets is to be present in the output set exactly

Changing something from iterating over a numpy array to vectorization

為{幸葍}努か 提交于 2020-01-15 08:15:32
问题 I am trying to speed up the piece of code below by vectorization: [rows,cols] = flow_direction_np.shape elevation_gain = np.zeros((rows,cols), np.float) for [i, j], flow in np.ndenumerate(flow_direction_np): try: if flow == 32: elevation_gain[i - 1, j - 1] = elevation_gain[i - 1, j - 1] + sediment_transport_np[i, j] elif flow == 64: elevation_gain[i - 1, j] = elevation_gain[i - 1, j] + sediment_transport_np[i, j] elif flow == 128: elevation_gain[i - 1, j + 1] = elevation_gain[i - 1, j + 1] +

Compute sum of series

怎甘沉沦 提交于 2020-01-15 05:56:07
问题 I need to compute the sum of this series I need the output this way: If n = 3; x = function_name(n) I need to get x = 11. If n = 5; x = function_name(n) I need to get x = 45 . I believe I need a for-loop to iterate; but am finding it difficult to iterate the increment value itself. 回答1: inc=2; sum=1; next=1; n=input('what is n?\n'); for i=2:n next=next+inc; sum=sum+next; inc=inc+2; end disp('sum is '); disp(sum); 回答2: I guess you want the sum of the cumsum of the differences d of the numbers:

Vectorizing rep and seq in R

孤者浪人 提交于 2020-01-14 12:59:30
问题 I am trying to accomplish two things. First if I have a vector 1:5 I want to get a matrix (or two vectors) indicating the unique combinations of these elements including twice the same number but excluding repetitions. Right now I can do this using a matrix: foo <- matrix(1:5,5,5) cbind(foo[upper.tri(foo,diag=TRUE)],foo[lower.tri(foo,diag=TRUE)]) [,1] [,2] [1,] 1 1 [2,] 1 2 [3,] 2 3 [4,] 1 4 [5,] 2 5 [6,] 3 2 [7,] 1 3 [8,] 2 4 [9,] 3 5 [10,] 4 3 [11,] 1 4 [12,] 2 5 [13,] 3 4 [14,] 4 5 [15,] 5