Average of several (different, across) structures

24 views (last 30 days)
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 Comment
MARCO BERGONZI
MARCO BERGONZI on 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

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 18 Apr 2019
Edited: Stephen23 on 18 Apr 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.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!