Help with cusum and cellfun
Show older comments
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
1 Comment
Walter Roberson
on 19 May 2017
Accepted Answer
More Answers (1)
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]}
Categories
Find more on Cell Arrays 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!