Arrays 对于matlab,我有一个包含5个句子的数组,我需要找到每个句子中的特殊字符数
我的代码是:Arrays 对于matlab,我有一个包含5个句子的数组,我需要找到每个句子中的特殊字符数,arrays,string,matlab,character,conditional,Arrays,String,Matlab,Character,Conditional,我的代码是: my5Sentences={'Windows machines are better than Macs.','The Intel core i7 4770k is a great processor.','My email is rjoshi8@drexel.edu','I go to Drexel','I am writing this in MATLAB & and I am writing this code for Engr-180'}; for i=1:leng
my5Sentences={'Windows machines are better than Macs.','The Intel core i7 4770k is a great processor.','My email is rjoshi8@drexel.edu','I go to Drexel','I am writing this in MATLAB & and I am writing this code for Engr-180'};
for i=1:length(my5Sentences)
fprintf('The total number of characters in this sentence is %i\n',length(my5Sentences{i}))
end
这部分代码显示每个句子中有多少个字符,但现在我需要找出每个句子中有多少个特殊字符。我不确定这段代码是什么样子的。顺便说一句,感谢PearsonArtPhoto帮我解答了最初的问题 试试正则表达式:
[f]=regexp(my5Sentences{i},'([^a-zA-Z_0-9\s])','tokens')
如果需要索引,请检查f的其他输出,如果某些字符处理错误,请检查regexp的文档。您可以使用以下方法轻松完成此操作: 我假设“特殊字符”不是指字母或数字。否则,请将
'\W'
更改为所需的内容。例如,如果空格和句点不算作特殊字符,请使用
cellfun(@(str) numel(regexp(str, '[^a-z_A-Z0-9\s.]')), my5Sentences)
使用
regexp
:我认为提问者和另一个人来自同一个班级!哈哈!
cellfun(@(str) numel(regexp(str, '[^a-z_A-Z0-9\s.]')), my5Sentences)