Average of several (different, across) structures

23 visualizaciones (últimos 30 días)
ErikaZ
ErikaZ el 18 de Abr. de 2019
Comentada: MARCO BERGONZI el 10 de Sept. de 2020
I have 10 structures with 6 fields, each with 3D arrays. All of them have the same organization.
I want to do the average of the 10 structures, as a result I would have 1 mean structure with 6 fields, each with 3D arrays.
For example, I have:
Subject_1_RMSE_angle =
struct with fields:
narxwalk: [17×10×16 double]
EMGwalk: [17×10×16 double]
narxup: [17×10×16 double]
EMGup: [17×10×16 double]
narxdown: [17×10×16 double]
EMGdown: [17×10×16 double]
Subject_2_RMSE_angle =
struct with fields:
narxwalk: [17×10×16 double]
EMGwalk: [17×10×16 double]
narxup: [17×10×16 double]
EMGup: [17×10×16 double]
narxdown: [17×10×16 double]
EMGdown: [17×10×16 double]
Subject_3_RMSE_angle =
struct with fields:
narxwalk: [17×10×16 double]
EMGwalk: [17×10×16 double]
narxup: [17×10×16 double]
EMGup: [17×10×16 double]
narxdown: [17×10×16 double]
EMGdown: [17×10×16 double]
then do the mean, and have a results as:
Subject_MEANs_RMSE_angle =
struct with fields:
narxwalk: [17×10×16 double]
EMGwalk: [17×10×16 double]
narxup: [17×10×16 double]
EMGup: [17×10×16 double]
narxdown: [17×10×16 double]
EMGdown: [17×10×16 double]
Thank you.
  1 comentario
MARCO BERGONZI
MARCO BERGONZI el 10 de Sept. de 2020
function [outmode,outmean,outmedian,outprcile]=mfromstruct(nomestruttura,nomecampo,percentile_voluto)
pos=length(nomestruttura);
x=zeros(pos,1);
t=numel(nomestruttura(1).(nomecampo));
outmode=nomestruttura(1).(nomecampo)*0;
outmean=outmode;
outmedian=outmode;
outprcile=outmode;
for j=1:t
for i=1:pos
x(i)=nomestruttura(i).(nomecampo)(j);
end
outmode(j)=mode(x);
outmean(j)=mean(x);
outmedian(j)=median(x);
outprcile(j)=prctile(x,percentile_voluto);
end
end

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 18 de Abr. de 2019
Editada: Stephen23 el 18 de Abr. de 2019
Fake data in scalar structures:
S1 = struct('A',randi(99,4,3,2),'B',randi(99,4,3,2));
S2 = struct('A',randi(99,4,3,2),'B',randi(99,4,3,2));
S3 = struct('A',randi(99,4,3,2),'B',randi(99,4,3,2));
Concatenate into one non-scalar structure (which they should be anyway):
SX = [S1,S2,S3];
Concatenate data from each field, calculate mean:
C = fieldnames(SX);
SZ = struct(); % scalar structure
for k = 1:numel(C)
F = C{k};
SZ.(F) = mean(cat(4,SX.(F)),4);
end
SZ is a scalar output structure containing the means of the input scalar structures.

Más respuestas (0)

Categorías

Más información sobre Structures 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