Structural Arrays
Show older comments
I have a 1x10 struc array with 2 fields. These fields have approximately 16000 elements and I would like to add each element from every array of the respective field so that in the end, I have an array of another say 16000 elements containing the cumulative sum fo the first 10 arrays (phew!) . For example: if one of the fields is weight, I would like to add the values of weight of 10 arrays, say (1 to 10kg for the first row, 11-20kg for the second, and so on till the 16000th row) for every row in order to get a sum (of 55kg, 155kg, respectively till the 16000th value). After much trial with functions, I'm unable to write a code for such a result. Any help would be much appreciated. Thanks in advance!
Answers (1)
Sean de Wolski
on 6 Dec 2011
I would just unpack the struct into matrices and do the math on the matrices. Repack them into a struct at the end if necessary.
doc fieldnames %to extract with a for loop
doc struct2cell %to convert to a cell then a matrix.
doc cell2mat
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!