Average of several (different, across) structures

조회 수: 23 (최근 30일)
ErikaZ
ErikaZ 2019년 4월 18일
댓글: MARCO BERGONZI 2020년 9월 10일
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
MARCO BERGONZI
MARCO BERGONZI 2020년 9월 10일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 4월 18일
편집: Stephen23 2019년 4월 18일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by