0
votes

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?

1
sometimes it's better to use a for-loop, much more readable. - Amro
Why are you passing three inputs to arrayfun? Your anonymous function only expects 2. Seems like your d is in the wrong spot - Suever
@Amro When to decide which to use? - Léo Léopold Hertz 준영
When you can't make sense of your own code - Suever
@Suever Thank you! It was one mistake. I updated the body. - Léo Léopold Hertz 준영

1 Answers

4
votes

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