Borrar filtros
Borrar filtros

Group-wise mean in multi-dimentional array

4 visualizaciones (últimos 30 días)
Brian
Brian el 14 de Oct. de 2015
Comentada: dpb el 14 de Oct. de 2015
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!

Respuesta aceptada

dpb
dpb el 14 de Oct. de 2015
Editada: dpb el 14 de Oct. de 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 comentarios
Brian
Brian el 14 de Oct. de 2015
Thank you so much for the wonderful answer (and tips on coding). I am very new to coding, so these constructive tips are invaluable.
I was able to convert my cell array into 121*515 with the squeeze function. The rest worked beautifully. Could I ask how could I attain the same "averaging every 5" result when my starting array is 256*256*515? The 256*256 dimension is a format of image.
dpb
dpb el 14 de Oct. de 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!!! :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by