bsxfun

Efficient operations of big non-sparse matrices in Matlab

五迷三道 提交于 2019-12-07 11:21:16
问题 I need to operate in big 3-dim non-sparse matrices in Matlab. Using pure vectorization gives a high computation time. So, I have tried to split the operations into 10 blocks and then parse the results. I got surprised when I saw the the pure vectorization does not scale very well with the data size as presented in the following figure. I include an example of the two approaches. % Parameters: M = 1e6; N = 50; L = 4; K = 10; % Method 1: Pure vectorization mat1 = randi(L,[M,N,L]); mat2 = repmat

bsxfun implementation in solving a min. optimization task

一笑奈何 提交于 2019-12-07 09:13:34
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).^2 + (LBR(:,3) - var3).^2); Which returns the point p where this min -problem is fulfied. Finally I

Optimize nested for loop for calculating xcorr of matrix rows

家住魔仙堡 提交于 2019-12-06 11:05:20
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] = max(d); LAG(i,j)=I-1; d=xcorr(y,x); d=d(:,F:(2*F)-1); [M,I] = max(d); LAG(j,i)=I-1; end end end

replace repmat with bsxfun in MATLAB

北城余情 提交于 2019-12-06 07:59:41
问题 In the following function i want to make some changes to make it fast. By itself it is fast but i have to use it many times in a for loop so it takes long. I think if i replace the repmat with bsxfun will make it faster but i am not sure. How can i do these replacements function out = lagcal(y1,y1k,source) kn1 = y1(:); kt1 = y1k(:); kt1x = repmat(kt1,1,length(kt1)); eq11 = 1./(prod(kt1x-kt1x'+eye(length(kt1)))); eq1 = eq11'*eq11; dist = repmat(kn1,1,length(kt1))-repmat(kt1',length(kn1),1);

Efficient operations of big non-sparse matrices in Matlab

守給你的承諾、 提交于 2019-12-05 13:42:20
I need to operate in big 3-dim non-sparse matrices in Matlab. Using pure vectorization gives a high computation time. So, I have tried to split the operations into 10 blocks and then parse the results. I got surprised when I saw the the pure vectorization does not scale very well with the data size as presented in the following figure. I include an example of the two approaches. % Parameters: M = 1e6; N = 50; L = 4; K = 10; % Method 1: Pure vectorization mat1 = randi(L,[M,N,L]); mat2 = repmat(permute(1:L,[3 1 2]),M,N); result1 = nnz(mat1>mat2)./(M+N+L); % Method 2: Split computations result2 =

replace repmat with bsxfun in MATLAB

柔情痞子 提交于 2019-12-04 11:48:19
In the following function i want to make some changes to make it fast. By itself it is fast but i have to use it many times in a for loop so it takes long. I think if i replace the repmat with bsxfun will make it faster but i am not sure. How can i do these replacements function out = lagcal(y1,y1k,source) kn1 = y1(:); kt1 = y1k(:); kt1x = repmat(kt1,1,length(kt1)); eq11 = 1./(prod(kt1x-kt1x'+eye(length(kt1)))); eq1 = eq11'*eq11; dist = repmat(kn1,1,length(kt1))-repmat(kt1',length(kn1),1); [fixi,fixj] = find(dist==0); dist(fixi,fixj)=eps; mult = 1./(dist); eq2 = prod(dist,2); eq22 = repmat(eq2

MATLAB tutorial for programmers [closed]

寵の児 提交于 2019-12-02 14:10:52
I'm getting some new students soon, who will be writing MATLAB code. They're new to MATLAB, but they have experience coding in Java and C++. I'm going to have them go through the Getting Started section of the MATLAB help. In addition, I want to give a small tutorial with the goal to prevent them from making some of the most common mistakes people make when switching to MATLAB (e.g. "MATLAB starts counting at 1"), and show them some features that they may not be aware of when coming from other languages (e.g. "you can subtract a scalar directly from an array, and for vectors, there's bsxfun").

Is bsxfun really applied element-wise?

一个人想着一个人 提交于 2019-12-02 06:14:39
问题 Suppose that I have the following function: function x = printAndKeepX(x, y) x y end and I invoke bsxfun like so: bsxfun(@printAndKeepX, 1:4, 1); Were bsxfun really element-by-element, I would expect printAndKeepX to be called 4 times, with the arguments (x, y) being (1, 1) , (2, 1) , (3, 1) and (4, 1) , respectively. But the output shows that it is called just once with (x, y) being ([1 2 3 4], 1) : x = 1 2 3 4 y = 1 Why? How can I know what's considered an "element"? Edit: The documentation

Matlab's bsxfun() code

五迷三道 提交于 2019-12-02 00:09:13
问题 What does this do? u = [5 6]; s = [1 1]; data1 =[randn(10,1) -1*ones(10,1)]; data2 =[randn(10,1) ones(10,1)]; data = [data1; data2]; deviance = bsxfun(@minus,data,u); deviance = bsxfun(@rdivide,deviance,s); deviance = deviance .^ 2; deviance = bsxfun(@plus,deviance,2*log(abs(s))); [dummy,mini] = min(deviance,[],2); Is there an equivalent way of doing this without bsxfun? 回答1: The function BSXFUN will perform the requested element-wise operation (function handle argument) by replicating

Matlab's bsxfun() code

时光怂恿深爱的人放手 提交于 2019-12-01 21:23:24
What does this do? u = [5 6]; s = [1 1]; data1 =[randn(10,1) -1*ones(10,1)]; data2 =[randn(10,1) ones(10,1)]; data = [data1; data2]; deviance = bsxfun(@minus,data,u); deviance = bsxfun(@rdivide,deviance,s); deviance = deviance .^ 2; deviance = bsxfun(@plus,deviance,2*log(abs(s))); [dummy,mini] = min(deviance,[],2); Is there an equivalent way of doing this without bsxfun? The function BSXFUN will perform the requested element-wise operation (function handle argument) by replicating dimensions of the two input arguments so that they match each other in size. You can avoid the use of BSXFUN in