Help with cusum and cellfun
1 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Walter Roberson
2017-5-19
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 个)
Greg Dionne
2017-6-8
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Speed Up Statistical Computations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!