Calculating means in large matrix using logical indexing

조회 수: 1 (최근 30일)
Ernst Jan
Ernst Jan 2014년 2월 28일
댓글: Ernst Jan 2014년 2월 28일
Hi there,
I have a large data matrix in which I want to calculate the average of elements satisfying certain conditions. The matrix has around 15 million rows.
There are two columns which are stored as a vector containing the conditions to be met:
elementIDs 15m x 1 (Containing the numbers 1 to 9664)
gridIDs 15m x 1 (Containing the numbers 1 to 3)
I want to know the average Reynolds number of each element in each grid. The Reynolds numbers are stored in:
Re 15m x 1 (Containing doubles)
The results are to be stored in a matrix with each element a row and each grid a column:
meanRe = 9664 x 3.
To illustrate this problem example I wrote the following:
% Initiate test data
n_rows = 150000 % In practice 15 000 000
elementIDs = randi(9664,n_rows,1);
gridIDs = randi(3,n_rows,1);
Re = rand(n_rows,1).*1000;
% Pre-allocate space
meanRe = zeros(9664,3);
% Timer
tic
% Loop over subsets to speed up the process
for kk = 1:3
% Select subset using logical indexing
Re_temp = Re(gridIDs==kk);
elementIDs_temp = elementIDs(gridIDs==kk);
% Loop over each element
for ii = 1:9664
% Calculate mean Reynolds using logical index
meanRe(ii,kk) = mean(Re_temp(elementIDs_temp==ii));
end
end
toc
Although for the amount of data the code runs fairly quick I still have to wait several minutes. Is their anyway to speed this code up significantly?

채택된 답변

Matt J
Matt J 2014년 2월 28일
편집: Matt J 2014년 2월 28일
subs=[elementIDs, gridIDs];
totals=accumarray(subs,Re);
counts=accumarray(subs,ones(size(Re)));
meanRe=totals./counts;
  댓글 수: 1
Ernst Jan
Ernst Jan 2014년 2월 28일
Great! I am wondering about the inner structure of this function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by