Reputation: 141110
I am expanding the arrayfun
code of the thread To Find Double Sequence With Transforms in Matlab? for a list of vectors in cellfun
(DD
).
Pseudocode
DD = {[9 1 5 6 6 6 5 1 1 0 7 7 7 7 7 8], [1 1 1 4], [7 7 7]};
d = cellfun(@(i) diff(diff([0 i 0]) == 0), DD, 'Uniform', false);
y = cellfun(@(z) ...
arrayfun(@(i,j) DD{i}(i:j), find(z>0), find(z<0), 'Uniform', false), ...
d, 'Uniform', false););
Expected output
y = { {[6 6 6], [1 1], [7 7 7]}, ...
{[1 1 1]}, ...
{[7 7 7]} };
Error
Index exceeds matrix dimensions.
Error in findDoubleSequenceAnonFunction>@(i,j)DD{i}(i:j)
Error in
findDoubleSequenceAnonFunction>@(z)arrayfun(@(i,j)DD{i}(i:j),find(z>0),find(z<0),'Uniform',false)
Error in findDoubleSequenceAnonFunction (line 5)
y = cellfun(@(z) ...
Comments
d = cellfun(...
. I am applying the function diff(diff(...
in cellfun
. It should be ok. y = cellfun(...
. Need to have cellfun
here because have the again a cell of vectors in d
. Somehow, the cellfun-arrayfun is complicating. How can you have cellfun-arrayfun combination here?
Upvotes: 0
Views: 146
Reputation: 124563
Just use a for-loop, easier to read:
XX = {[9 1 5 6 6 6 5 1 1 0 7 7 7 7 7 8], [1 1 1 4], [7 7 7]};
YY = cell(size(XX));
for i=1:numel(XX)
x = XX{i};
d = diff(diff([0 x 0]) == 0);
YY{i} = arrayfun(@(i,j) x(i:j), find(d>0), find(d<0), 'Uniform',false);
end
Result:
>> celldisp(YY)
YY{1}{1} =
6 6 6
YY{1}{2} =
1 1
YY{1}{3} =
7 7 7 7 7
YY{2}{1} =
1 1 1
YY{3}{1} =
7 7 7
Upvotes: 4