vectorization

Vectorize weighted sum matlab

一个人想着一个人 提交于 2019-12-12 02:11:43
问题 I was trying to vectorize a certain weighted sum but couldn't figure out how to do it. I have created a simple minimal working example below. I guess the solution involves either bsxfun or reshape and kronecker products but I still have not managed to get it working. rng(1); N = 200; T1 = 5; T2 = 7; A = rand(N,T1,T2); w1 = rand(T1,1); w2 = rand(T2,1); B = zeros(N,1); for i = 1:N for j1=1:T1 for j2=1:T2 B(i) = B(i) + w1(j1) * w2(j2) * A(i,j1,j2); end end end A = B; 回答1: You could use a

Trivial/impossible algorithm challenge in Octave/Matlab - iterations memory

自闭症网瘾萝莉.ら 提交于 2019-12-11 20:29:15
问题 There is an input vector with allowed set of values {-2,-1,0,1,2} , e.g. input = [2 2 2 2 0 1 0 -1 0 -2 -2 -1 0 1] Vector is scanned iteratively from start to end and at each point a certain transition table is realized, based on the current value of the vector, and a certain accumulated value determined by previous iterations. This value is incremented/decremented by a requested step (3 here) up to the top value (9 here) beyond which it cannot go. The output vector represents certain ongoing

reformulating for loop with vectorization or other approach - octave

点点圈 提交于 2019-12-11 19:13:12
问题 Is there any way to vectorize (or reformulate) each body of the loop in this code: col=load('col-deau'); %load data h=col(:,8); % corresponding water column dates=col(:,3); % and its dates %removing out-of-bound data days=days(h~=9999.000); h=h(h~=9999.000); dates=sort(dates(h~=9999.000)); [k,hcat]=hist(h,nbin); %making classes (k) and boundaries of classes (hcat) of water column automatically dcat=1:15; % make boundaries for dates for k=1:length(dcat)-1 % Loop for each date class ii=find

How to create a matrix in Matlab with every entry being the output of a bivariate function

邮差的信 提交于 2019-12-11 18:23:51
问题 I want to create a 4 x 4 matrix with each entry representing f(x,y) where both x and y take values 0, 1, 2 and 3. So the first entry would be f(0,0), all the way to f(3,3). The function f(x,y) is: 3 * cos(0*x + 0*y) + 2 * cos(0*x + 1*y) + 3 * cos(0*x + 2*y) + 8 * cos(0*x + 3*y) + 3 * cos(1*x + 0*y) + 25 * cos(1*x + 1*y) + 3 * cos(1*x + 2*y) + 8 * cos(1*x + 3*y) + 3 * cos(2*x + 0*y) + 25 * cos(2*x + 1*y) + 3 * cos(2*x + 2*y) + 8 * cos(2*x + 3*y) + 3 * cos(3*x + 0*y) + 25 * cos(3*x + 1*y) + 3 *

Applying a function to an array using Numpy when the function contains a condition

我的梦境 提交于 2019-12-11 17:56:37
问题 I am having a difficulty with applying a function to an array when the function contains a condition. I have an inefficient workaround and am looking for an efficient (fast) approach. In a simple example: pts = np.linspace(0,1,11) def fun(x, y): if x > y: return 0 else: return 1 Now, if I run: result = fun(pts, pts) then I get the error ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() raised at the if x > y line. My inefficient workaround

How to efficiently compute logsumexp of upper triangle in a nested loop?

柔情痞子 提交于 2019-12-11 17:56:13
问题 I have a nested for loop that iterates over rows of the weight matrix and applies logsumexp to the upper triangular portion of the outer addition matrix from these weights rows. It is very slow so I'm trying to figure out how to speed this up by either vectorizing or taking out the loops in lieu of matrix operations. ''' Wm: weights matrix, nxk W: updated weights matrix, nxn triu_inds: upper triangular indices of Wxy outer matrix ''' for x in range(n-1): wx = Wm[x, :] for y in range(x+1, n):

elimination of consecutive regions

▼魔方 西西 提交于 2019-12-11 17:51:58
问题 I need to effectively eliminate consecutive regions in vector "a" or better in rows/columns of matrix "A" with length of separate ones regions greater than positive integer N <= length(A): See following example: N = 2 % separate consecutive regions with length > 2 are zeroed a = [0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1] a_elim = [0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1] or 2D case: N = 2 A = [1 0 1 … 1 1 0 … 1 1 0 … 0 0 1 … 1 1 1] % elimination over columns A_elim= 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 % elimination

Matlab multiple Halir ellipse fitting using vectorization

試著忘記壹切 提交于 2019-12-11 17:45:34
问题 I regularly have upwards of 10^8 sets of ellipse fitting data to solve coming out of monte-carlo simulations by looping through the Halir & Flusser (Page 4, Figure 2) ellipse fitting algorithm. Is there a way of vectorizing the process/algorithm/function, so that I can send the lot at once to the function and it returns 10^8 solutions to the 10^8 sets? I want to stick with Halir & Flusser as it is very robust. Having used other ellipse-fitting methods, this one always returns ellipses I can

Write a for/while loop with “if/else” in a more elegant way?

余生长醉 提交于 2019-12-11 17:44:41
问题 I've written this code : A is a nXm matrix [nA, mA] = size(A); currentVector(nA,mA) = 0; for i = 1: nA for j = 1 : mA if A (i,j) ~= 0 currentVector(i,j) = ceil(log10( abs(A(i,j)) )); else currentVector(i,j) = 0; end end end How can I write the above code in a more "matlab" way ? Are there any shortcuts for if/else and for loops ? for example in C : int a = 0; int b = 10; a = b > 100 ? b : a; Those if/else conditions keeps reminding me of C and Java . Thanks 回答1: %# initialize a matrix of

Optimize computation time for PDF approximation based on Kernel Density Estimation

佐手、 提交于 2019-12-11 17:38:33
问题 I have a code to find the pdf's approximation of a vector based on the formula for kernel estimation: I implemented this formula in the code below (see previous question). However, that code takes long time to run (two loops are used). Could you see the below code and help me to make it faster? This is the code: function pdf_est=KDE() close all; %%Random values of 20 pixels, range=[1 256] data=randi([1 256],1,20)-1; %// changed: "-1" %% Estimate histogram%%%%% pdf_est=zeros(1,256); z=256; for