Calculating means in large matrix using logical indexing

1 次查看(过去 30 天)
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;

更多回答(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