Help with cusum and cellfun
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Stephen Thompson
am 19 Mai 2017
Beantwortet: Greg Dionne
am 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
Akzeptierte Antwort
Walter Roberson
am 19 Mai 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
Weitere Antworten (1)
Greg Dionne
am 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]}
0 Kommentare
Siehe auch
Kategorien
Mehr zu Speed Up Statistical Computations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!