phraseblanks
phrasemat =您好,你好吗?
大家好!
怎么回事?
WHazzup?
短语1有4个空白
短语2有3个空白
短语3有2个空白
短语4有0个空白
新短语是:
您好:功放;和&安培;如何和放大器;是&放大器;你
喜和放大器;还有和放大器;!每个人
如何&安培;是&安培;它&安培;去
WHazzup?
所以我制作了剧本“phraseblanks.m”:
phrasemat = char('Hello and how are you?', ...
'Hi there everyone!', 'How is it going?', 'WHazzup?')
[r, c] = size(phrasemat);
for i = 1:r
phrasemat_new = cell(r, c);
howmany = countblanks(phrasemat(i, :));
fprintf('Phrase %d had %d blanks\n', i, howmany);
phrasemat(i,:) = strrep(phrasemat(i,:),' ','&')
phrasemat_new{i,:} = [phrasemat(i,:)];
end
fprintf('Changing one is %s\n', eval('phrasemat_new'));
脚本“countblanks.m”:
function num = countblanks(phrase)
% countblanks returns the # of blanks in a trimmed string
% Format: countblanks(string)
num = length(strfind(strtrim(phrase), ' '));
end
我一直有错误。
请帮帮我..
答案 0 :(得分:0)
我稍微修改了 phraseblanks.m 以便它可以正常工作。
phrasemat = {'Hello and how are you?', ...
'Hi there everyone!', ...
'How is it going?', ...
'WHazzup?'};
r = numel(phrasemat);
phrasemat_new = cell(1, r);
for i = 1:r
howmany = countblanks(phrasemat{i});
fprintf('Phrase %d had %d blanks\n', i, howmany);
phrasemat{i} = strrep(phrasemat{i}, ' ', '&');
phrasemat_new(i) = phrasemat(i);
end
fprintf('Changing one is %s\n', phrasemat_new{:});
显然它可以用更好的,更“matlaby-way”的方式编写,但我不想在你的原始版本上走得太远。你也可以考虑使用正则表达式,因为如果你必须彼此相邻,你想把它们当作一个空格。