vectorization

While loop Vectorization

六月ゝ 毕业季﹏ 提交于 2020-01-07 00:35:54
问题 I would like to know if there is some way to vectorize this code. I tried so hard to do it... but failed. while (delta_F > e) && (i < maxLoop) x1 = x0+d; y0 = f(x0); y1 = f(x1); if y1 < y0 x0= x1; d = a*d; else vF = [vF;x1]; d = -b*d; end i = i + 1; if length(vF) > 1 ultm = vF(end); pultm = vF(end-1); delta_F = abs(ultm+pultm)/2; end end It's a simple implementation of the Rosenbrock method for finding the min of a function. 回答1: In general, Matlab's vectorization works over fixed-size arrays

While loop Vectorization

让人想犯罪 __ 提交于 2020-01-07 00:34:27
问题 I would like to know if there is some way to vectorize this code. I tried so hard to do it... but failed. while (delta_F > e) && (i < maxLoop) x1 = x0+d; y0 = f(x0); y1 = f(x1); if y1 < y0 x0= x1; d = a*d; else vF = [vF;x1]; d = -b*d; end i = i + 1; if length(vF) > 1 ultm = vF(end); pultm = vF(end-1); delta_F = abs(ultm+pultm)/2; end end It's a simple implementation of the Rosenbrock method for finding the min of a function. 回答1: In general, Matlab's vectorization works over fixed-size arrays

Need to vectorize solution that using nested loops (transform data frame from long to wide format)

浪子不回头ぞ 提交于 2020-01-06 21:45:08
问题 I have following data frame and need to do transformation from long into wide format: symbol side price 1 A B 1 2 A S 2 3 B B 3 4 C B 4 5 B S 5 Explanation: for every symbol must exists two lines with side B and side S. Need to find these lines and transform them into wide format: [symbol, first-comed side (B or S), price of side B, price of side S] If one line exists but another is missing, then put NA value to appropriate price value. For example if line with side B exists, but with side S

Need to vectorize solution that using nested loops (transform data frame from long to wide format)

若如初见. 提交于 2020-01-06 21:44:46
问题 I have following data frame and need to do transformation from long into wide format: symbol side price 1 A B 1 2 A S 2 3 B B 3 4 C B 4 5 B S 5 Explanation: for every symbol must exists two lines with side B and side S. Need to find these lines and transform them into wide format: [symbol, first-comed side (B or S), price of side B, price of side S] If one line exists but another is missing, then put NA value to appropriate price value. For example if line with side B exists, but with side S

creating this matrix in matlab

早过忘川 提交于 2020-01-06 18:01:15
问题 Given A=[a_1 a_2 a_3 ... a_n] How to make this? [a1 ... a100] [a2 ... a101] ... [an-100+1 ... an] I want to not use for-loop here since I want to speed it up. Thank you. 回答1: You can use: n = numel(A); m = 100; I = bsxfun(@plus, 1:m, (0:n-m).'); B = A(I); As a sidenote: The for loop doesn't perform that bad: B = zeros(n-m+1, m); for i = 1:size(B) B(i,:) = A(i:i+m-1); end As far as my testing goes, it is slower only by a factor of 4 and this calculation should hardly be a bottleneck in your

creating this matrix in matlab

浪子不回头ぞ 提交于 2020-01-06 18:00:29
问题 Given A=[a_1 a_2 a_3 ... a_n] How to make this? [a1 ... a100] [a2 ... a101] ... [an-100+1 ... an] I want to not use for-loop here since I want to speed it up. Thank you. 回答1: You can use: n = numel(A); m = 100; I = bsxfun(@plus, 1:m, (0:n-m).'); B = A(I); As a sidenote: The for loop doesn't perform that bad: B = zeros(n-m+1, m); for i = 1:size(B) B(i,:) = A(i:i+m-1); end As far as my testing goes, it is slower only by a factor of 4 and this calculation should hardly be a bottleneck in your

MATLAB vectorize

别等时光非礼了梦想. 提交于 2020-01-06 07:15:14
问题 I was wondering if anyone could help me vectorize this piece of code. fr_bw is a matrix. for i=1:height for j=1:width [min_w, min_w_index] = min(w(i,j,:)); mean(i,j,min_w_index) = double(fr_bw(i,j)); sd(i,j,min_w_index) = sd_init; end end 回答1: I can't help you with this sif (match == 0) stuff -- if it's supposed to be if (match == 0) you're not changing match so it could be brought outside the loop. Otherwise, how about this: [min_w, min_w_index] = min(w, [], 3); r = repmat((1:height)',1

How to use _mm_extract_epi8 function? [duplicate]

核能气质少年 提交于 2020-01-06 04:43:06
问题 This question already has an answer here : _mm_extract_epi8(…) intrinsic that takes a non-literal integer as argument (1 answer) Closed 11 months ago . I am using _mm_extract_epi8 (__m128i a, const int imm8) function, which has const int parameter. When I compile this c++ code, getting the following error message: Error C2057 expected constant expression __m128i a; for (int i=0; i<16; i++) { _mm_extract_epi8(a, i); // compilation error } How could I use this function in loop? 回答1: First of

Vectorized Update numpy array using another numpy array elements as index

試著忘記壹切 提交于 2020-01-06 03:39:06
问题 Let A,C and B be numpy arrays with the same number of rows. I want to update 0th element of A[0], 2nd element of A[1] etc. That is, update B[i]th element of A[i] to C[i] import numpy as np A = np.array([[1,2,3],[3,4,5],[5,6,7],[0,8,9],[3,7,5]]) B = np.array([0,2,1,2,0]) C = np.array([8,9,6,5,4]) for i in range(5): A[i, B[i]] = C[i] print ("FOR", A) A = np.array([[1,2,3],[3,4,5],[5,6,7],[0,8,9],[3,7,5]]) A[:,B[:]] = C[:] print ("Vectorized, A", A) Output: FOR [[8 2 3] [3 4 9] [5 6 7] [0 8 5]

Is there a way to calculate the following specified matrix by avoiding loops? in R or Matlab

走远了吗. 提交于 2020-01-05 10:31:56
问题 I have an N-by-M matrix X , and I need to calculate an N-by-N matrix Y : Y[i, j] = sum((X[i,] - X[j,]) ^ 2) 0 <= i,j <= N For now, I have to use nested loops to do it with O(n 2 ). I would like to know if there's a better way, like using matrix operations. more generally, sum(....) can be a function, fun(x1,x 2) of which x1 , x2 are M-by-1 vectors. 回答1: you can use expand.grid to get a data.frame of possible pairs: X <- matrix(sample(1:5, 50, replace=TRUE), nrow=10) row.ind <- expand.grid(1