bsxfun

Avoiding sub2ind and ind2sub

末鹿安然 提交于 2020-04-10 05:03:29
问题 I need to access several indices around a certain point in 3D. For example, for point ( x1 , y1 , z1 ) I need to get all the indices of its 3x3x3 neighborhood such that ( x1 , y1 , z1 ) is centered. For neighborhood of size 3, I do it with [x,y,z] = meshgrid(-1:1,-1:1,-1:1); x_neighbors = bsxfun(@plus,x,x1); y_neighbors = bsxfun(@plus,y,y1); z_neighbors = bsxfun(@plus,z,z1); Here, I center x1 , y1 , z1 to (0,0,0) by adding the distances from ( x1 , y1 , z1 ) to any point in the 3x3x3 box.

How much faster is implicit expansion compared with bsxfun?

僤鯓⒐⒋嵵緔 提交于 2020-01-19 05:37:09
问题 As commented by Steve Eddins, implicit expansion (introduced in Matlab R2016b) is faster than bsxfun for small array sizes, and has similar speed for large arrays: In R2016b, implicit expansion works as fast or faster than bsxfun in most cases. The best performance gains for implicit expansion are with small matrix and array sizes. For large matrix sizes, implicit expansion tends to be roughly the same speed as bsxfun . Also, the dimension along which expansion takes place may have an

Generalized Matrix Product

时光总嘲笑我的痴心妄想 提交于 2020-01-02 02:24:22
问题 I'm fairly new to MATLAB. Normal matrix multiplication of a M x K matrix by an K x N matrix -- C = A * B -- has c_ij = sum(a_ik * b_kj, k = 1:K) . What if I want this to be instead c_ij = sum(op(a_ik, b_kj), k = 1:K) for some simple binary operation op ? Is there any nice way to vectorize this in MATLAB (or maybe even a built-in function)? EDIT: This is currently the best I can do. % A is M x K, B is K x N % op is min C = zeros(M, N); for i = 1:M: C(i, :) = sum(bsxfun(@min, A(i, :)', B)); end

Vectorizing nested loops in matlab using bsxfun and with GPU

南笙酒味 提交于 2019-12-28 16:26:01
问题 For loops seem to be extremely slow, so I was wondering if the nested loops in the code shown next could be vectorized using bsxfun and maybe GPU could be introduced too. Code %// Paramaters i = 1; j = 3; n1 = 1500; n2 = 1500; %// Pre-allocate for output LInc(n1+n2,n1+n2)=0; %// Nested Loops - I for x = 1:n1 for y = 1:n1 num = ((n2 ^ 2) * (L1(i, i) + L2(j, j) + 1)) - (n2 * n * (L1(x,i) + L1(y,i))); LInc(x, y) = L1(x, y) + (num/denom); LInc(y, x) = LInc(x, y); end end %// Nested Loops - II for

How to convert a nested loop into parfor loop

℡╲_俬逩灬. 提交于 2019-12-25 18:12:46
问题 This is my from my MATLAB script. function [ Im ] = findBorders( I ) Im = false(size(I)); I = padarray(I, [1, 1], 1); [h w] = size(Im); bkgFound = false; for row = 1 : h for col = 1 : w if I(row + 1, col + 1) bkgFound = false; for i = 0:2 for j = 0:2 if ~I(row + i, col + j) Im(row, col) = 1; bkgFound = true; break; end; end; if bkgFound break; end; end; end; end; end; end So, I need to convert it to parfor loop, to run into GPU. I need help. I read some articles, but have no idea about how to

Replicate elements from an array into a different array in Matlab

心已入冬 提交于 2019-12-24 23:33:00
问题 In Matlab I have an array "latencies" (size 1x11) and a cell array "Latencies_ms" (size 1x298). latencies is a smaller part of Latencies_ms , i.e. the values in latencies exist within Latencies_ms . I want to be able to find each value in latencies inside Latencies_ms and then add 1000 to that value and repeat this 8 times inside Latencies_ms . So for example, latencies = [62626 176578 284690 397708 503278 614724 722466] and (a small sample of Latencies_ms) Latencies_ms = {3458, 62626, 123456

How to project a new point to PCA new basis?

こ雲淡風輕ζ 提交于 2019-12-23 18:13:38
问题 For example, I have 9 variables and 362 cases. I've made PCA calculation, and found out that first 3 PCA coordinates are enough for me. Now, I have new point in my 9-dimensional structure, and I want to project it to principal component system coordinate. How to get its new coordinates? %# here is data (362x9) load SomeData [W, Y] = pca(data, 'VariableWeights', 'variance', 'Centered', true); %# orthonormal coefficient matrix W = diag(std(data))\W; % Getting mean and weights of data (for

MATLAB: bsxfun unclear. Want to accelerate minimum distance between segments

谁都会走 提交于 2019-12-23 12:12:49
问题 Using MATLAB, Imagine a Nx6 array of numbers which represent N segments with 3+3=6 initial and end point coordinates. Assume I have a function Calc_Dist( Segment_1, Segment_2 ) that takes as input two 1x6 arrays, and that after some operations returns a scalar, namely the minimal euclidean distance between these two segments. I want to calculate the pairwise minimal distance between all N segments of my list, but would like to avoid a double loop to do so. I cannot wrap my head around the

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

bsxfun implementation in solving a min. optimization task

╄→гoц情女王★ 提交于 2019-12-23 00:50:48
问题 I really need help with this one. I have to matrices L1 and L2 , both are (500x3) of size. First of all, I compute the difference of every element of each column of L1 from L2 as follows: lib1 = bsxfun(@minus, L1(:,1)',L2(:,1)); lib1=lib1(:); lib2 = bsxfun(@minus, L1(:,2)',L2(:,2)); lib2=lib2(:); lib3 = bsxfun(@minus, L1(:,3)',L2(:,3)); lib3=lib3(:); LBR = [lib1 lib2 lib3]; The result is this matrix LBR . Then I have a min -problem to solve: [d,p] = min((LBR(:,1) - var1).^2 + (LBR(:,2) - var2