MATLAB: Reduce computational time when calling cellfun

cellfuncomputational timeoptimization

I have:
for i=1:length(cellarray)
id1=cellfun(@(x)any(ismember('1',x)),cellarray{i})
id2=cellfun(@(x)any(ismember('2',x)),cellarray{i});
id3=cellfun(@(x)any(ismember('3',x)),cellarray{i});
...
end
which I would like to rewrite as something like:
for i=1:length(cellarray)
value=cellfun(@(x)x(1),cellarray{i})
id1=any(ismember('1',value));
id2=any(ismember('2',value));
id3=any(ismember('3',value));
...
end
in order to try to reduce computational time. Here,
cellarray={'AA','KK','AKs','QQ','AKo',...};
The problem is, that the first approach correctly evaluates each cell and return the results into a vector id1, id2, … whereas the second approach considers all elements in the cell array simultaneously. Changing "any(ismember(…))" to "ismember(…)" also only evaluates the entire array.
How do I make sure that each cell is evaluated? Say the cell array has 5 elements, then id1 should have 5 elements containing 0's or 1's.

Best Answer

for i = 1:length(cellarray)
S = cellarray{i};
id1 = any(strfind(S, '1'));
id2 = any(strfind(S, '2'));
id3 = any(strfind(S, '3'));
...
end
If the cell array is very large and you look for a few characters only, this approach could be more efficient, when you want to process the complete cell without a loop:
id1 = ~strcmp(cellarray, strrep(cellarray, '1', '*'));
...