How to do operations on nested cell array?

I have a nested cell array named 'events'.Each cell contains either 1 or more than 1 cells which in turn contains time series.
I want to write a code where, if the cell contains more than 1 cell arrays of time series, I want to find the standard deviation of each of those time series and retain only the time series with the highest standard deviation.
For ex: For the case of events {6,1} which contains 3 time series, I want to find the std of events{6,1}{1,1}, events{6,1}{2,1} and events{6,1}{3,1} and only retain the time series with the highest std and delete the rest.
How can I do this?
Thanks
events.PNG event_2.PNG event_3.PNG

Antworten (2)

Walter Roberson
Walter Roberson am 16 Jan. 2020
Bearbeitet: Walter Roberson am 16 Jan. 2020

0 Stimmen

for K = 1 : numel(events)
[~, idx] = max(cellfun(@std, events{K}));
Events{K} = events{K}(idx);
end
I would not bother trying to do it without a loop. It is possible, yes, but avoiding the loop requires calculating the std twice because inside the loop it is not possible to get at the second output of max()

1 Kommentar

maruljay
maruljay am 16 Jan. 2020
I get the error:
Error using cellfun
Input #2 expected to be a cell array, was double instead.

Melden Sie sich an, um zu kommentieren.

Turlough Hughes
Turlough Hughes am 16 Jan. 2020
Bearbeitet: Turlough Hughes am 16 Jan. 2020

0 Stimmen

This should hopefully work but i ament able to test it myself right now as I'm away from my computer. The idea is to pass onecell at a time from the first level through a subfunction called myfun. myfun subsequently uses cellfun again to check each subcell, it finds the one with max standard deviation and should return that one.
EventsStdMax = cellfun(@(x) myfun(x),events,'UniformOutput',false)
function out = myfun(onecell)
[~,idx] = max(cellfun(@(x) std(x), onecell));
out = onecell{idx};
end
Edit: As Walter pointed out.

6 Kommentare

out = subcell{idx};
should probably be
out = onecell{idx};
Turlough Hughes
Turlough Hughes am 16 Jan. 2020
Thanks.
maruljay
maruljay am 16 Jan. 2020
I get the error:
Error using cellfun
Input #2 expected to be a cell array, was double instead.
Walter Roberson
Walter Roberson am 17 Jan. 2020
Your screen snapshot shows the entries in events are cell arrays, but one of the entries between 28 and 58 is not a cell array (possibly it is unexpectedly [] instead of {})
maruljay
maruljay am 17 Jan. 2020
Or is it because the time series is stored as a double like the screenshot shows 1x4217 double?
No, according to your images , the first 27 entries in events are not timeseries and are instead cell arrays containing timeseries. But somewhere in events you have an entry which is not a cell array.
cellfun(@class, events, 'uniform', 0)
and look for one that does not say 'cell'

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 15 Jan. 2020

Kommentiert:

am 17 Jan. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by