bsxfun

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

optimization of pairwise L2 distance computations

我的梦境 提交于 2019-12-11 13:23:56
问题 I need help optimizing this loop. matrix_1 is a ( n x 2) int matrix and matrix_2 is a ( m x 2), m & n very. index_j = 1; for index_k = 1:size(Matrix_1,1) for index_l = 1:size(Matrix_2,1) M2_Index_Dist(index_j,:) = [index_l, sqrt(bsxfun(@plus,sum(Matrix_1(index_k,:).^2,2),sum(Matrix_2(index_l,:).^2,2)')-2*(Matrix_1(index_k,:)*Matrix_2(index_l,:)'))]; index_j = index_j + 1; end end I need M2_Index_Dist to provide a ( (n*m) x 2) matrix with the index of matrix_2 in the first column and the

Vectorization of min distance in kernel

社会主义新天地 提交于 2019-12-11 08:49:40
问题 I have an Nx2 array K1 with the location of N keypoints and a 3 dimensional WxHx3 array Kart1(width,height,coordinates) that maps coordinates to every pixel of an image. For every keypoint in K1 I want to read the location of the pixel in Kart1 and evaluate the coordinates (search for the min/max or calculate the mean) in a 3x3 kernel around it and assign a value to the current pixel in KPCoor1 . My current approach looks like this: for ii=1:length(K1(:,1)) %for every keypoint in K1 MinDist

Subscript indices must either be real positive integers or logicals

家住魔仙堡 提交于 2019-12-11 04:33:28
问题 I write a function to sum each row of a matrix which have three rows. Then use a matrix which have one row and three columns to divide the previous result. But I keep getting that error. I know the subscript should not be a decimal or negative number. But I still can not find the culprit. Please help, thanks. % mean_access_time(ipinfo_dist, [306, 32, 192]) % 'ipinfo_dist' is a matrix which have three rows and column is not fixed. function result = mean_access_time(hash_mat, element_num)

How to use Matlab's bsxfun to solve cumulative sum

此生再无相见时 提交于 2019-12-10 18:46:58
问题 I have the following (slow) piece of code: % A is n-by-m matrix % B is n-by-m-by-d matrix % C is n-by-m-by-d matrix % R is 1-by-d vector A=zeros(n,m); for i=1:d A = A + sum(B(:,:,1:i),3).*(R(i)-C(:,:,i)); end I would like to make it more efficient by using the magical bsxfun to lose the loop. Can you show me how to do that? 回答1: This way - A = sum(cumsum(B,3).*bsxfun(@minus,permute(R,[1 3 2]),C),3) With size parameters n,m,d as 200 each, the runtimes were -----------------------------------

How to sum parts of a matrix of different sizes, without using for loops?

那年仲夏 提交于 2019-12-10 13:35:17
问题 I have a relatively large matrix NxN (N~20,000) and a Nx1 vector identifying the indices that must be grouped together. I want to sum together parts of the matrix, which in principle can have a different number of elements and non-adjacent elements. I quickly wrote a double for-loop that works correctly but of course it is inefficient. The profiler identified these loops as one of the bottlenecks in my code. I tried to find a smart vectorization method to solve the problem. I explored the

Finding index-positions after -spatial- matrix multiplication. bsxfun implemented

烂漫一生 提交于 2019-12-10 11:12:24
问题 I need help finding some index-positions of a matrix and two vectors after a complicated matrix multiplication, please bear with me and read what I have first, my question comes at the end. I have two matrices L1 and L2 : L1 = firstMatrix; L2 = secondMatrix; I need to compute the difference (column-wise) of every single value from L1 with all the values of L2 , again, in column-wise form, this is done as follows: step one lib1 = bsxfun(@minus, L1(:,1)',L2(:,1)); lib1=lib1(:); lib2 = bsxfun(

Optimize nested for loop for calculating xcorr of matrix rows

冷暖自知 提交于 2019-12-10 11:04:44
问题 I have 2 nested loops which do the following: Get two rows of a matrix Check if indices meet a condition or not If they do: calculate xcorr between the two rows and put it into new vector Find the index of the maximum value of sub vector and replace element of LAG matrix with this value I dont know how I can speed this code up by vectorizing or otherwise. b=size(data,1); F=size(data,2); LAG= zeros(b,b); for i=1:b for j=1:b if j>i x=data(i,:); y=data(j,:); d=xcorr(x,y); d=d(:,F:(2*F)-1); [M,I]

Trouble Understanding Sliding Window for a column of a matrix, general and specific case in Matlab

吃可爱长大的小学妹 提交于 2019-12-08 07:59:41
问题 I am noob and i found very fragmentated information on stack on Slinding Window. I have a mXn matrix, where m is fixed(latitude, longitude, ax, ay, az), n could change from different logs. 1) How can i create a sliding window only for az without extracting the vector and then analyzing it? 2) If i want to save all the rows where the az standard deviation go over a defined thresholds how can i do that? 3) If logs length is not fixed how can i deal with that? (ex. one file contains 932 rows,

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

耗尽温柔 提交于 2019-12-08 05:56:27
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 over it, but I can't get it to work otherwise. It's still much faster than any other method I've tried,