vectorization

vectorizing a matlab / octave FOR loop

送分小仙女□ 提交于 2019-12-23 04:49:26
问题 I'm a little confused as to how to vectorize this for loop see code below: array1=[xfreq_orig,yamp_orig,yamp_inv,phase_orig] %frequency, amplitudes, phases to use t_rebuilt=linspace(0,2*pi,44100) aa_sig_rebuilt_L=zeros(1,length(t_rebuilt)); aa_sig_combined_L=zeros(1,length(t_rebuilt)); sig_full_L=zeros(1,length(t_rebuilt)); for kk=1:1:numel(xfreq_orig); aa_sig_rebuilt_L = array1(kk, 2)*cos ((array1(kk,1))*t_rebuilt+(array1(kk, 4))); aa_sig_combined_L = aa_sig_combined_L + aa_sig_rebuilt_L;

Is MATLAB's bsxfun the best? Python's numpy.einsum?

久未见 提交于 2019-12-23 04:20:52
问题 I have a very large multiply and sum operation that I need to implement as efficiently as possible. The best method I've found so far is bsxfun in MATLAB, where I formulate the problem as: L = 10000; x = rand(4,1,L+1); A_k = rand(4,4,L); tic for k = 2:L i = 2:k; x(:,1,k+1) = x(:,1,k+1)+sum(sum(bsxfun(@times,A_k(:,:,2:k),x(:,1,k+1-i)),2),3); end toc Note that L will be larger in practice. Is there a faster method? It's strange that I need to first add the singleton dimension to x and then sum

Build distance matrix in a vectorized way (without loop) from Latitude Longitude coordinates

对着背影说爱祢 提交于 2019-12-23 04:01:36
问题 I would like to come up with a faster way to create a distance matrix between all lat lon pairs. This QA addresses doing a vectorized way with standard Linear Algebra, but without Lat Lon coordinates. In my case these lat longs are farms. Here is my Python code, which for the full data set (4000 (lat, lon)'s) takes at least five minutes. Any ideas? > def slowdistancematrix(df, distance_calc=True, sparse=False, dlim=100): """ inputs: df returns: 1.) distance between all farms in miles 2.)

How can I vectorize this python function to take an array of points (as pairs of cartesian coordinates)?

心不动则不痛 提交于 2019-12-23 03:32:06
问题 I have this function which checks if a point (passed as pair of cartesian coordinates) is inside the trapezoid (passed as list of tuples of vertices coordinates) def inside(point_coordinates, trapezoid_vertices): x,y = point_coordinates is_inside = False x_1, y_1 = trapezoid_vertices[0] for i in range(1, 4): x_2, y_2 = trapezoid_vertices[i] if y > min(y_1, y_2): if y <= max(y_1, y_2): if x <= max(x_1, x_2): if y_1 != y_2: x_intersect = (y - y_1) * (x_2 - x_1) / (y_2 -y_1) + x_1 if x_1 == x_2

Octave code for gradient descent using vectorization not updating cost function correctly

倾然丶 夕夏残阳落幕 提交于 2019-12-23 02:38:08
问题 I have implemented following code for gradient descent using vectorization but it seems the cost function is not decrementing correctly.Instead the cost function is increasing with each iteration. Assuming theta to be an n+1 vector, y to be a m vector and X to be design matrix m*(n+1) function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) m = length(y); % number of training examples n = length(theta); % number of features J_history = zeros(num_iters, 1); error = ((theta'

Better way to extract all the rows from a Matrix A that contain an element of a matrix B

二次信任 提交于 2019-12-22 18:42:47
问题 Matrix A is my starting matrix it holds the data logged from my MPU6050 and GPS on an SD Card (Latitude, Longitude, Time, Ax, Ay, Az, Gx,Gy,Gz). I calculated the standard deviation of Az for window size of 5 and identified all the elements that satisfy a condition (>threshold). Then in a matrix "large_windows" i stored the index of all the Az in the window that satisfy the condition. From matrix "large_windows" i calculated a new matrix B with all the rows from matrix A that contain the

efficient way of performing integral on an image

百般思念 提交于 2019-12-22 18:10:21
问题 I have a 2D array (typical size about 400x100) as shown (it looks like a trapezium because elements in the lower right are nan). For each element in the array, I want to perform a numerical integral along the column for several elements (of the order of ~10 elements). In physics language think of the colour as the magnitude of the force, and I want to find the work done by calculating th integral of Fdz. I can use a double for-loop and use trap to do the job, but are there other more

Creating new numpy arrays based on condition

十年热恋 提交于 2019-12-22 17:59:35
问题 I have 2 numpy arrays: aa = np.random.rand(5,5) bb = np.random.rand(5,5) How can I create a new array which has a value of 1 when both aa and bb exceed 0.5? 回答1: With focus on performance and using two methods few aproaches could be added. One method would be to get the boolean array of valid ones and converting to int datatype with .astype() method. Another way could involve using np.where that lets us select between 0 and 1 based on the same boolean array. Thus, essentially we would have

Numba - How to fill 2D array in parallel

一曲冷凌霜 提交于 2019-12-22 13:48:36
问题 I have a function that operates on a 2D matrix on float64(x,y). Basic concept: for each combination of rows (no. rows choose 2) count the number of positiv values after subtraction (row1 - row2). In a 2Dmatrix of int64(y,y) store this value in index [row1,row2] if value is above a certain threshold and [row2,row1] if below. I've implemented that and decorated it with @njit(parallel=False), that works fine @njit(parallel=True) seems to give no speedup. Trying to speed up the whole thing I had

vectorized backtest creation of pandas DataFrame

天大地大妈咪最大 提交于 2019-12-22 11:35:17
问题 This is my final try after posting several questions that might have been badly addressed/described. I want to achive this DataFrame as result: Signal price buy_units sell_units cashflow balance Index 0 0 40 0 0 0 100000 1 1 50 2000 0 -100000 0 2 -1 100 0 -2000 +200000 200000 3 1 50 4000 0 -200000 0 4 -1 70 0 -4000 +280000 280000 It's a stock trading strategy backtest. When Signal is ==1 buy_units is equal to current balance (value from prior row) divided by price . balance then becomes