Group-wise mean in multi-dimentional array
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a cell array of the dimension 1 x 121 x 515. It represents 515 sets of 1 x 121 frequency data. Since every 5 sets (along the 515) belongs in a group, I would like to take the element-wise mean of the frequency data every 5 sets (along the 515 axis). In other words, averaging the frequency data for each group so the final output will be 1x121x103. How should I do so? I tried to use the reshape feature to turn my array into 5 x 121 x 103, but the result transformation is incorrect when I compare the numbers in the pre- and post- transformed numbers.
Here is what I did:
O_npsaverageAirT=reshape(npsaverageAir,5,121,103);
I believe I can use the following to take my group-wise mean once the previous step is worked out. Is that right?
O_npsaverageAir=mean(O_npsaverageAirT,1);
I similarly have a 256*256*515 array that needs the same "average-every-5" operation along the 515-axis. I believe the solution should be similar to this one?
Thanks so much!
0 commentaires
Réponse acceptée
dpb
le 14 Oct 2015
Modifié(e) : dpb
le 14 Oct 2015
"cell array of the dimension 1 x 121 x 515. It represents 515 sets of 1 x 121..."
First, why the complication of a cell array: convert to a 2D array of 515x121; then the manipulations are straightforward to average over groups of N (N divisible into M, the number of rows/columns comprising the sets dimension).
If the array were that 121x515, then it's (in steps to see the process) --
[r,c]=size(x); % get the initial size (121x515 here, general case)
N=5; % the number over which averaging desired
m=x.'; % transpose to put the grouping dimension by row
m=reshape(m,N,[]); % get columns containing N sequential elements
m=mean(m); % take average of those groups of N
m=reshape(m,c,[]); % split back up the means by length of signal
m will now be same number of rows but averaged over N successive columns to reduce that by a factor of 1/N. And, of course, the above can all be strung together; it's just harder initially to read...
m=reshape(mean(reshape(x.',N,[])),c,[]);
When writing such expressions, don't begin from the leftmost part trying to keep track of paren's, etc. Instead start from the inside out as in the above steps; when you have the first reshape() expression, then surround it by the next set of parens for the next outer function and so on until done...it's best to practice on these kinds of things with small datasets at the command line that are small enough to see easily the full array but aren't symmetric so can watch the shape changes. A good test case here would be something like
x=rand(4,9);
N=3;
which would be nine realizations of a very short spectrum of length four but the actual size is immaterial to the operations.
2 commentaires
dpb
le 14 Oct 2015
3D isn't so simple to make use of the internal storage order by simply rearranging the 2D array. There may be some clever tricks possible but probably here you're just as well of to simply write the looping solution.
[r,c,p]=size(x); % row, column plane dimensions
M=N-1; % number planes less one
m=zeros(r,c,p/N); % preallocate, N as before
j=0; % counter for working plane result
for i=1:N:p
j=j+1;
m(:,:,j)=mean(:,:,i:i+M),3);
end
NB: Air code; no guarontees!!! :)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!