Average across structures and fields
18 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, I currently have a set of data that is organized into a main structure with 3 fields (walking_01, walking_02, walking_03). Each of those 3 fields has a sub-structure with 101 fields (LHipAngles, LKneeAngles, etc.). Each of these 101 fields are made up of a double array containing 101 rows x any number of columns. I would like to find the average of each of the 101 fields across all of the main structure's fields (i.e. average of LHipAngles across walking_01, walking_02, and walking_03).
0 commentaires
Réponses (1)
Jan
le 12 Déc 2022
It was a bad idea to hide an index in the field names as in walking_01. Using an array with a real index is smarter. This is fixed easily:
ModOutLStride_x.walking = cat(1, ModOutLStride_x.walking_01, ...
ModOutLStride_x.walking_02, ...
ModOutLStride_x.walking_03));
By the way, the "L" in the name of the structure looks like you hide the information about the side in the name of the variable. The code is more flexible, if you store the side information separately.
After the above creation of the array:
Data = ModOutLStride_x; % Abbreviation
keyList = fieldnames(Data.walking(1));
nCycle = numel(Data.walking);
for iKey = 1:numel(keyList)
key = keyList{iKey};
value = 0;
for iCycle = 1:nCycle
value = value + Data.walking(iCycle).(key);
end
Data.mean.(key) = value / nCycle;
end
2 commentaires
Jan
le 14 Déc 2022
My code sums up these variables, and you want to concatenate them. So you can modify my code easily:
...
valueC = cell(1, nCycle);
for iCycle = 1:nCycle
valueC{iCycle} = Data.walking(iCycle).(key);
end
Data.All.(key) = cat(2, valueC{:}); % Or maybe cat along 1st or 3rd dim?
...
Voir également
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!