How To Vectorize a For loop

8 ビュー (過去 30 日間)
Chris Porter
Chris Porter 2018 年 10 月 31 日
編集済み: Bruno Luong 2018 年 10 月 31 日
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 Center および File ExchangeMATLAB Coder についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by