Mean of a subset of columns for every row

1 visualización (últimos 30 días)
Perla González Pereyra
Perla González Pereyra el 12 de Nov. de 2019
Editada: dpb el 13 de Nov. de 2019
I know there are several question like this, but i want my code be more dinamic and work for every input matrix.
So the problems is this: I have a matrix 34X3000, and I want tho get the mean of 10 columns for every row. The output will be 34x300.
So far I had this, I know the error is for the index but I don't no how to do it. Can you help me?
TotalRecordingTimeSec=length(Data);
binsize=10;%Entrada
timeline=0:binsize:TotalRecordingTimeSec;
AllcelsFR=[];
AllFR=[];
for bb=1:size(Data,1)
for cc=1:size(timeline,2)-1
fh=timeline(cc);
th=timeline(cc+1);
BinData=Data(Data>fh & Data<th);
AllFR=[AllFR;nanmedian(BinData)];
end
AllcelsFR=[AllcelsFR AllFR];
end

Respuesta aceptada

dpb
dpb el 12 de Nov. de 2019
N=10;
M=reshape(mean(reshape(Data.',N,[])),size(Data,2)/N,[]).';
  2 comentarios
Perla González Pereyra
Perla González Pereyra el 12 de Nov. de 2019
This work pretty well, thank you so much. I have to read reshape again
dpb
dpb el 12 de Nov. de 2019
Editada: dpb el 13 de Nov. de 2019
Utilizes that memory storage order in MATLAB is row major and builtin functions such as mean are vectorized to operate by column.
Start with a very small sample dataset like 3x12 or so with N=4, say, that you can verify results by inspection. Then observe each step sequentially at the command line.
(VERY powerful technique)

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.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by