script ...
function benchie
% Create a large, mixed type cell array
myCell = repmat( { 1, 2, 3, 'test', 1 , 'abc';
4, 5, 6, 'foob', 'a', 'def' }, 10000, 5 );
% Create anonymous functions for TIMEIT
f1 = @() usingStrcmp(myCell);
f2 = @() usingUnique(myCell);
f3 = @() usingLoops(myCell);
f4 = @() usingISA(myCell);
f5 = @() usingIsClass(myCell);
% Timing of different methods
timeit(f1)
timeit(f2)
timeit(f3)
timeit(f4)
timeit(f5)
end
function usingStrcmp(myCell)
% The original method
types = cellfun( @class, myCell, 'uni', false );
typesOK = all( strcmp(repmat(types(1,:), size(types,1), 1), types), 1 );
types = types(1, :);
end
function usingUnique(myCell)
% Using UNIQUE instead of STRCMP, as suggested by rahnema1
types = cellfun( @class, myCell, 'uni', false );
[type,~,idx]=unique(types);
u = unique(reshape(idx,size(types)),'rows');
if size(u,1) == 1
% consistent
else
% not-consistent
end
end
function usingLoops(myCell)
% Using loops instead of CELLFUN. Move onto the next column if a type
% difference is found, otherwise continue looping down the rows
types = cellfun( @class, myCell(1,:), 'uni', false );
typesOK = true(size(types));
for c = 1:size(myCell,2)
for r = 1:size(myCell,1)
if ~strcmp( class(myCell{r,c}), types{c} )
typesOK(c) = false;
continue
end
end
end
end
function usingISA(myCell)
% Using ISA instead of converting all types to strings. Suggested by Sam
types = cellfun( @class, myCell(1,:), 'uni', false );
for ii = 1:numel(types)
typesOK(ii) = all(cellfun(@(x)isa(x,types{ii}), myCell(:,ii)));
end
end
function usingIsClass(myCell)
% using the same method as found in CELL2MAT. Suggested by CitizenInsane
ncols = size(myCell, 2);
typesOK = false(1, ncols);
types = cell(1, ncols);
for ci = 1:ncols
cellclass = class(myCell{1, ci});
ciscellclass = cellfun('isclass', myCell(:, ci), cellclass);
typesOK(ci) = all(ciscellclass);
types{ci} = cellclass;
end
end
:
R2015b
usingStrcmp: 0.8523 secs
usingUnique: 1.2976 secs
usingLoops: 1.4796 secs
usingISA: 10.2670 secs
usingIsClass: 0.0131 secs % RAPID!
R2017b
usingStrcmp: 0.8282 secs
usingUnique: 1.2128 secs
usingLoops: 0.4763 secs % ZOOOOM! (Relative to R2015b)
usingISA: 9.6516 secs
usingIsClass: 0.0093 secs % RAPID!
, , .
, ( ), MATLAB (2017b), > 65% 50% , !
:
( ) .MATLAB .
: CitizenInsane, , , , , Matlab cell2mat.
: usingIsClass.