Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 将字符串中三个字母单词的第一个和最后一个字母大写_Matlab - Fatal编程技术网

Matlab 将字符串中三个字母单词的第一个和最后一个字母大写

Matlab 将字符串中三个字母单词的第一个和最后一个字母大写,matlab,Matlab,我试图将字符串中只有三个字母的单词的第一个字母和最后一个字母大写。到目前为止,我已经试过了 spaces = strfind(str, ' '); spaces = [0 spaces]; lw = diff(spaces); lw3 = find(lw ==4); a3 = lw-1; b3 = spaces(a3+1); b4 = b3 + 2 ; str(b3) = upper(str(b3)); str(b4) = upper(str(b4); 我们必须先找到3个字母的单

我试图将字符串中只有三个字母的单词的第一个字母和最后一个字母大写。到目前为止,我已经试过了

spaces = strfind(str, ' ');
spaces = [0 spaces]; 
lw = diff(spaces); 
lw3 = find(lw ==4); 
a3 = lw-1; 
b3 = spaces(a3+1); 
b4 = b3 + 2 ; 
str(b3) = upper(str(b3)); 
str(b4) = upper(str(b4);

我们必须先找到3个字母的单词的位置,这就是前4行代码的位置,然后其他人试图找到它,这样它就能找到第一个和最后一个字母的位置,然后将它们大写。

我会使用正则表达式来识别3个字母的单词,然后使用
regexprep
与匿名函数来执行案例转换

str = 'abcd efg hijk lmn';

% Custom function to capitalize the first and last letter of a word
f = @(x)[upper(x(1)), x(2:end-1), upper(x(end))];

% This will match 3-letter words and apply function f to them
out = regexprep(str, '\<\w{3}\>', '${f($0)}')

%   abcd EfG hijk LmN
str='abcd efg hijk lmn';
%自定义函数,用于将单词的第一个和最后一个字母大写
f=@(x)[上(x(1)),x(2:end-1),上(x(end))];
%这将匹配3个字母的单词,并对其应用函数f
out=regexprep(str,'\','${f($0)}')
%abcd EfG hijk LmN

我会使用正则表达式来识别3个字母的单词,然后使用
regexprep
结合匿名函数来执行大小写转换

str = 'abcd efg hijk lmn';

% Custom function to capitalize the first and last letter of a word
f = @(x)[upper(x(1)), x(2:end-1), upper(x(end))];

% This will match 3-letter words and apply function f to them
out = regexprep(str, '\<\w{3}\>', '${f($0)}')

%   abcd EfG hijk LmN
str='abcd efg hijk lmn';
%自定义函数,用于将单词的第一个和最后一个字母大写
f=@(x)[上(x(1)),x(2:end-1),上(x(end))];
%这将匹配3个字母的单词,并对其应用函数f
out=regexprep(str,'\','${f($0)}')
%abcd EfG hijk LmN

正则表达式绝对是一种选择。我将建议一种稍微不同的方法,即使用
regexpi
tokenextensts
标志返回索引:

str = 'abcd efg hijk lmn';

% Tokenize the words and return the first and last index of each
idx = regexpi(str, '(\<w{3}\>)', 'tokenExtents');

% Convert those indices to upper case
str([idx{:}]) = upper(str([idx{:}]));
结果是一个177575字符的字符串,包含5531个3字母的单词。我使用
timeit
检查使用
regexprep
regexpi
tokenextens
的执行时间。使用
regexpi
要快一个数量级:

regexpi = 0.013979s
regexprep = 0.14401s

正则表达式无疑是一种发展方向。我将建议一种稍微不同的方法,即使用
regexpi
tokenextensts
标志返回索引:

str = 'abcd efg hijk lmn';

% Tokenize the words and return the first and last index of each
idx = regexpi(str, '(\<w{3}\>)', 'tokenExtents');

% Convert those indices to upper case
str([idx{:}]) = upper(str([idx{:}]));
结果是一个177575字符的字符串,包含5531个3字母的单词。我使用
timeit
检查使用
regexprep
regexpi
tokenextens
的执行时间。使用
regexpi
要快一个数量级:

regexpi = 0.013979s
regexprep = 0.14401s

问题是什么?要大写的
str
中每个单词的第一个字母的位置不是
空格(lw3)+1
?我建议不要混淆您自己的变量名。你不会从短变量名中得到任何东西,使用一些有意义的东西。问题是什么?要大写的
str
中每个单词的第一个字母的位置不是
spaces(lw3)+1
?我建议不要混淆你自己的变量名。短变量名不会带来任何好处,请使用有意义的名称。