MATLAB Answers

Averageing cell arrays in cell array

18 views (last 30 days)
Tim Kreitzberg
Tim Kreitzberg on 2 Mar 2021
Commented: Tim Kreitzberg on 2 Mar 2021
Hello I is there a easy way to average cell arrays contained by another cell array?
The programm has a main variable called subject, which contains the the data eegData, where the data is split into each electrode, which contains several variations of this data like eegSpecBandPerStim
the structure for this example is like:
i = number of Electrodes
j = number of Stimulus
k = number of Spectrum Bands (Alpha, Beta,[..])
n = data -> same length for each row in k
subject.eegData{1,i}.eegSpecBandPerStim{j,k}(n)
Is there a easier way to average the data in eegSpecBandPerStim over all electrodes/eegData Cells rather than for looping through i, j and k? I saw cellfun(), but Im getting errors because of the dot structure. This would be the most complicated example, the variable also stores cell arrays with structure of 1x7.
The result should be a cell with jxk structure and all data 1xn - it would only eleminate the dimension of the electrode.
Its technicnally also possible to create a variable with avaraged EEG data and generate the different variations based on this data earlier in the code, so this would be only for cleaning up the subject variable and move all EEG related data into the eegData struct + learning purposes.

Accepted Answer

Jan
Jan on 2 Mar 2021
Using loops is the direct, clean and efficient way to access nested structs.
cellfun works on cells only, not on nested structs. The code looks nicer with cellfun, but the execution time is growing remarkably.
Its technicnally also possible to create a variable with avaraged EEG data and generate the different variations based on this data earlier in the code, so this would be only for cleaning up the subject variable and move all EEG related data into the eegData struct + learning purposes
Yes, of course this is possible. But there is no chance for the readers to suggest a corresponding code, because we do not know any details.
  1 Comment
Tim Kreitzberg
Tim Kreitzberg on 2 Mar 2021
Thank you Jan!
The last paragraph was just for explanation, where the idea of the question came from. Thougth maybe someone knows a trick in MATLAB I haven´t learned yet.
Since the execution time seems to be slower I will stick to creating the data earlier in the code.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!