How to Apply Cellfun Vectors with Arrayfun, Matlab?

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


How can you have cellfun-arrayfun combination here?

Upvotes: 0

Views: 146

Answers (1)

Amro
Amro

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

Related Questions