要测试是否对于非常大的阵列可能更快但可能是这样的:
function [b] = IsTypeConsistentColumns(myCell)
%[
b = true;
try
for ci = 1:size(myCell, 2)
cell2mat(myCell(:, ci));
end
catch err
if (strcmpi(err.identifier, 'MATLAB:cell2mat:MixedDataTypes'))
b = false;
else
rethrow(err);
end
end
%]
end
这取决于cell2mat与你的字符串比较的速度有多快(即使这里没有使用cell2mat的结果).
请注意,如果类型不一致,则cell2mat将抛出错误(标识符:’MATLAB:cell2mat:MixedDataTypes’,message =’输入单元阵列的所有内容必须具有相同的数据类型.’)
编辑:限制为cellfun(‘isclass’,c,cellclass)测试
这里只使用在cell2mat例程中内部执行的类型一致性检查:
function [consistences, types] = IsTypeConsistentColumns(myCell)
%[
ncols = size(myCell, 2);
consistences = false(1, ncols);
types = cell(1, ncols);
for ci = 1:ncols
cellclass = class(myCell{1, ci});
ciscellclass = cellfun('isclass', myCell(:, ci), cellclass);
consistences(ci) = all(ciscellclass);
types{ci} = cellclass;
end
%]
end
随你测试案例myCell = repmat({1,2,3,’test’,1,’abc’; 4,5,6,’foob’,’a’,’def’},10000,5);
使用R2015b在我的计算机上大约需要0.0123秒……如果你想在第一个非一致列上失败,它甚至会更快(这里我正在测试它们)