Reduce computational time when calling cellfun
Ältere Kommentare anzeigen
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.
1 Kommentar
Are the elements of cellarray are strings? No, sorry, they are cells of cell strings, correct?
Please do not let us guess, but provide some sample data in valid Matlab syntax. And please insert them by editing the question, not as comment or answer.
Comments to the code:
- strfind is much more efficient than ismember(CHAR).
- Creating a bunch of variables called id1, id2, ... is a bad programming practize. id{1}, id{2}, ... would be more convenient.
- id1=any(ismember('1',x)),value) contains 2 left and 3 right parenthesis. Please fix this.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!