Difference between a 3D mean function vs a for loop; computational time for summing over a dimension?
Show older comments
Hi guys,
I have a pretty big 3 dimensional data (subject_img_3D_PC_cut variable; dimension is around 41*1million*35) and been running some averaging on this data. I have run two different methods of averaging, one using a 3D nanmean and the other using a 2D nanmean; and the computational times for the two different methods seem to diverge a lot especially when the data is much greater. What mechanism of the nanmean function (basically isnan function + sum function) creates the difference between 2d and 3d averaging?
heres my code:
% using nanmean
tic
grpavg = nanmean(subject_img_3D_PC_cut,3);
time_elapsed_nanmean = toc
%using for loop
tic
grpavg = nan(size(subject_img_3D_PC_cut,1),size(subject_img_3D_PC_cut,2),1);
for jk = 1:size(subject_img_3D_PC_cut,2)
grpavg(:,jk,1) = nanmean(subject_img_3D_PC_cut(:,jk,:),3);
end
time_elapsed_forloop = toc
Results for the smaller data
%results for 41*902629*25 data.
time_elapsed_nanmean =
4.8007
time_elapsed_forloop =
8.7792
and the results for the bigger data:
%results for 41*902629*35 data.
time_elapsed_nanmean =
237.1386
time_elapsed_forloop =
12.9893
1 Comment
Michelle Wu
on 14 Mar 2017
You could use the MATLAB Profiler to see a breakdown of the execution time for each function when running your code. Please refer to the following doc for more information on how to use the Profiler:
Answers (0)
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!