Help with cusum and cellfun

5 views (last 30 days)
Stephen Thompson
Stephen Thompson on 19 May 2017
Answered: Greg Dionne on 8 Jun 2017
I am running the cusum function via cellfun as below - however I can only get one value from the output. When run separately, cusum outputs an [iupper,ilower] however running it via cellfun generates only one value, presumably the second change whether upper or lower. I am interested in first change - so either a way of getting that as an output or getting both as an output.
I hope this is clear. Thanks.
output = cellfun(@shift_time, input, 'UniformOutput', false);
function y = shift_time(x)
y = cusum(x, 100, 10, 0, 1);
end

Accepted Answer

Walter Roberson
Walter Roberson on 19 May 2017
It would not be "the second change" that you got in that case: it would be the first output argument.
output = cellfun(@shift_time, input, 'UniformOutput', false);
function upper_lower = shift_time(x)
[iupper, ilower] = cusum(x, 100, 10, 0, 1);
upper_lower = {iupper, ilower};
end
  1 Comment
Stephen Thompson
Stephen Thompson on 19 May 2017
That doesn't seem to be it - let me explain it again.
I have a 200X1 cell, each entry with 60000 points. Running cusum on one entry produces this (figure). There are two change point detection, upper and lower limits. In this case, it is the lower limit change point that I am interested in, the first detection, but the first detection might also be the upper limit change point in other data sets.
I want to replicate the cusum function across the whole dataset. It need not necessarily be via cellfun. The output will have to include both the upper and lower limits so that I can preserve both and later pick the first one.
I hope this is clear.

Sign in to comment.

More Answers (1)

Greg Dionne
Greg Dionne on 8 Jun 2017
I realize this may be a bit late, but in case someone else is looking for a solution, this worked for me:
% make some random data
a{1} = cumsum(randn(100,1));
a{2} = cumsum(randn(200,1));
a{3} = cumsum(randn(300,1));
a{4} = cumsum(randn(400,1));
a{5} = cumsum(randn(500,1));
% get first upper and lower breakouts
[iupper, ilower] = cellfun(@(x) cusum(x, 100,10,0,1), a, 'UniformOutput',false)
% get first breakout (if it exists)
ifirst = cellfun(@getfirst, iupper, ilower, 'UniformOutput',false)
function ifirst = getfirst(iupper, ilower)
if isempty(iupper)
ifirst = ilower;
elseif isempty(ilower)
ifirst = iupper;
elseif iupper < ilower
ifirst = iupper;
else
ifirst = ilower;
end
end
Output (you may get different results due to randn(), but hopefully they make sense)
iupper =
1×5 cell array
{0×1 double} {0×1 double} {[166]} {[363]} {0×1 double}
ilower =
1×5 cell array
{0×1 double} {[31]} {0×1 double} {[189]} {[43]}
ifirst =
1×5 cell array
{0×1 double} {[31]} {[166]} {[189]} {[43]}

Tags

Community Treasure Hunt

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

Start Hunting!