user2305193
user2305193

Reputation: 2059

averaging over 3rd dim, within a cell array, as efficiently as possible

I haven't found a good solution for this problem, which takes forever and seems to be mainly due to storage of the data in a cell array (as far as I see).

I process movie data in this format:

[data{1:4}] = deal(int16(randi([0 255],200,400,100))); %200px, 200px, 100 frames, 4 similar movies

Where data represents 4 different, but similar movies, in a cell array. Now I would like to take the average of the 4 variables data{1:4}, frame by frame. This is what I came up with:

for frame = 1:size(data{ind},3)
  tmp = zeros(200,400,'int16');
  for ind = 1:4
    tmp = tmp + data{ind}(:,:,frame);
  end
  data_avg(:,:,frame) = tmp./4;
end

is there a more efficient (faster performing, without doubling the RAM usage) way to do this (I haven't found any)?

Upvotes: 2

Views: 60

Answers (1)

bla
bla

Reputation: 26069

the fastest approach will be to do:

data_avg= (data{1}+data{2}+data{3}+data{4})/4;

no need for a for loop.

This is slower: mean(double(cat(4,data{:})),4); because matlabs mean has an overhead inefficiency. your for loop is in between.

Upvotes: 3

Related Questions