How To Vectorize a For loop

6 次查看(过去 30 天)
I Have the following piece of code I want to vectorize.
function [Averages, Counts]=GetLocalAverage(X,numbins)
%%This function computes the local average of X,
%%i.e. if X=[1:1:10]'; numbins=5; then Averages=[3; 8];
Min=min(X)*(1-1e-6); %A little smaller than Minmimum value of X
Max=max(X)*(1+1e-6); %A little bigger than Maximum value of X
Inc=(Max-Min)/(numbins); %Width of bins
Edges=[Min:Inc:Max]; %The Edges of the Bins, Just for Troubleshooting
Index=floor((X(:,1)-Min)/Inc)+1; %Figure out which bin each element of X goes into
%Preallocate
Counts=zeros(numbins,1); %Running Tally of Number of elements in each bin
BinSum=zeros(numbins,size(X,2)); %Running Tally of sum of elements in each bin
for n=1:size(X,1)
Counts(Index(n))=Counts(Index(n))+1;
BinSum(Index(n),:)=BinSum(Index(n))+X(n,:);
end
Averages=BinSum./Counts;
end
The problem is that the X vector I have is enormous (10^9 elements), so the for loop is really slow. What I want is something like:
Counts(Index)=Counts(Index)+1;
BinSum(Index)=BinSum(Index)+X(Index);
But this doesn't work because there can be multiple entries in X with that go to the same value in Index, and then this implementation leads to Counts only having values of 0 or 1.

采纳的回答

Bruno Luong
Bruno Luong 2018-10-31
编辑:Bruno Luong 2018-10-31
Min=min(X)*(1-1e-6); %A little smaller than Minmimum value of X
Max=max(X)*(1+1e-6); %A little bigger than Maximum value of X
Inc=(Max-Min)/(numbins); %Width of bins
Edges=[Min:Inc:Max]; %The Edges of the Bins, Just for Troubleshooting
Index=floor((X(:,1)-Min)/Inc)+1; %Figure out which bin each element of X goes into
% This replace the for-loop
[I,C] = ndgrid(Index,1:size(X,2));
Averages = accumarray([I(:),C(:)],X(:))./accumarray(Index,1);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by