开发者

what function or loop do I have to use to average the matrix?

I want to find the average of all the matrix:

Data=(Data{1}+......+Data{n})/n) where Data{n} is a matrix of m*n..

Thank you sooo mu开发者_StackOverflowch


First, you convert your cell array into a 3D array, then you can take the average, like this:

tmp = cat(3,Data{:}); %# catenates the data, so that it becomes a m*n*z (or m*1*n)
averageData = mean(tmp,3); %# takes average along 3rd dimension

Note: if you get memory problems this way, and if you don't need to keep the variable Data around, you can replace tmp with Data and all will work fine.

Alternatively, if Data is simply a m*n numeric array

averageData = mean(Data,2);


If your cell array is really big, you might want to keep away from the above solution because of its memory usage. I'd then suggest using the utility mtimesx which is available from Matlab Central, here.

N = length(Data);
b = cell(N,1);
b(:) = {1};
averageData = mtimesx(Data,b)/N;

In the above example, I assumed that Data is a line-shaped cell array. I have never used personally mtimesx, this solution comes from there, where timing issues are also discussed.

Hope this helps.

A.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜