我有一个矩阵,有两列,值和类型。 我想检查数据类型
m1 = {'Value','Type'
'str','char';
'stra','Real';
'34','char';
'2','Bool';
'1','Bool'
}
如何返回与类型(reel,char,bool)不对应的值。 结果将是:
mError = 'stra'
'2'
答案 0 :(得分:2)
如果任何字符串可以接受为'char':
ind1 = strcmp(m1(:,2),'Real') & isnan(str2double(m1(:,1)));
%// Any string is acceptable as 'char', so no ind2
ind3 = strcmp(m1(:,2),'Bool') & ~ismember((str2double(m1(:,1))),[0 1]);
mError = m1(ind1|ind3,1)
如果数字字符串不能被接受为'char':
ind1 = strcmp(m1(:,2),'Real') & isnan(str2double(m1(:,1)));
ind2 = strcmp(m1(:,2),'char') & ~isnan(str2double(m1(:,1)));
ind3 = strcmp(m1(:,2),'Bool') & ~ismember((str2double(m1(:,1))),[0 1]);
mError = m1(ind1|ind2|ind3,1)