Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

matlab - How to average over a cell-array of arrays?

I have a cell array c of equal-sized arrays, i.e. size(c{n}) = [ m l ... ] for any n. How can I get the mean values (averaging over the cell array index n) for all array elements in one sweep? I thought about using cell2mat and mean but the former does not add another dimension but changes l to l*n. And looping manually of course takes like forever...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If all of your arrays are the same size, it makes more sense to store them in a matrix rather than a cell array. That makes it easier to perform operations across them, like taking the mean. You can convert your data to a matrix using the functions NDIMS and CAT:

dim = ndims(c{1});          %# Get the number of dimensions for your arrays
M = cat(dim+1,c{:});        %# Convert to a (dim+1)-dimensional matrix
meanArray = mean(M,dim+1);  %# Get the mean across arrays

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...