Average of several (different, across) structures

32 vues (au cours des 30 derniers jours)
ErikaZ
ErikaZ le 18 Avr 2019
Commenté : MARCO BERGONZI le 10 Sep 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 commentaire
MARCO BERGONZI
MARCO BERGONZI le 10 Sep 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

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 18 Avr 2019
Modifié(e) : Stephen23 le 18 Avr 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.

Plus de réponses (0)

Catégories

En savoir plus sur Structures 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!

Translated by