我试图使用regexprep函数来反转字符串中的字母。但是,当我尝试用所有字母执行此操作时,它不起作用。我意识到,如果我改变字母“A'到了' Z',它最终会将这封信改写回' Z'。因此,如果我输入了' bBcC',则输出将是' bBcC'而不是' yYxX'。
function w = change(doc)
new_claim = regexprep(doc, 'A', 'Z');
new_claim1 = regexprep(new_claim, 'B', 'Y');
new_claim2 = regexprep(new_claim1, 'C', 'X');
new_claim3 = regexprep(new_claim2, 'D', 'W');
new_claim4 = regexprep(new_claim3, 'E', 'V');
new_claim5 = regexprep(new_claim4, 'F', 'U');
new_claim6 = regexprep(new_claim5, 'G', 'T');
new_claim7 = regexprep(new_claim6, 'H', 'S');
new_claim8 = regexprep(new_claim7, 'I', 'R');
new_claim9 = regexprep(new_claim8, 'J', 'Q');
new_claim10 = regexprep(new_claim9, 'K', 'P');
new_claim11 = regexprep(new_claim10, 'L', 'O');
new_claim12 = regexprep(new_claim11, 'M', 'N');
new_claim13 = regexprep(new_claim12, 'N', 'M');
new_claim14 = regexprep(new_claim13, 'O', 'L');
new_claim15 = regexprep(new_claim14, 'P', 'K');
new_claim16 = regexprep(new_claim15, 'R', 'J');
new_claim17 = regexprep(new_claim16, 'S', 'I');
new_claim18 = regexprep(new_claim17, 'T', 'H');
new_claim19 = regexprep(new_claim18, 'U', 'G');
new_claim20 = regexprep(new_claim19, 'V', 'E');
new_claim21 = regexprep(new_claim20, 'W', 'D');
new_claim22 = regexprep(new_claim21, 'X', 'C');
new_claim23 = regexprep(new_claim22, 'Y', 'B');
new_claim24 = regexprep(new_claim23, 'Z', 'A');
new_claim25 = regexprep(new_claim24, 'a', 'z');
new_claim26 = regexprep(new_claim25, 'b', 'y');
new_claim27 = regexprep(new_claim26, 'c', 'x');
new_claim28 = regexprep(new_claim27, 'd', 'w');
new_claim29 = regexprep(new_claim28, 'e', 'v');
new_claim30 = regexprep(new_claim29, 'f', 'u');
new_claim31 = regexprep(new_claim30, 'g', 't');
new_claim32 = regexprep(new_claim31, 'h', 's');
new_claim33 = regexprep(new_claim32, 'i', 'r');
new_claim34 = regexprep(new_claim33, 'j', 'q');
new_claim35 = regexprep(new_claim34, 'k', 'p');
new_claim36 = regexprep(new_claim35, 'l', 'o');
new_claim37 = regexprep(new_claim36, 'm', 'n');
new_claim38 = regexprep(new_claim37, 'n', 'm');
new_claim39 = regexprep(new_claim38, 'o', 'l');
new_claim40 = regexprep(new_claim39, 'p', 'k');
new_claim41 = regexprep(new_claim40, 'r', 'j');
new_claim42 = regexprep(new_claim41, 's', 'i');
new_claim43 = regexprep(new_claim42, 't', 'h');
new_claim44 = regexprep(new_claim43, 'u', 'g');
new_claim45 = regexprep(new_claim44, 'v', 'e');
new_claim46 = regexprep(new_claim45, 'w', 'd');
new_claim47 = regexprep(new_claim46, 'x', 'c');
new_claim48 = regexprep(new_claim47, 'y', 'b');
new_claim49 = regexprep(new_claim48, 'z', 'a');
w = new_claim49;
答案 0 :(得分:0)
使用这个简单的功能可以实现您所描述的内容:
function doc=change(doc);
doc(doc>='a')=char(25-doc(doc>='a')+2*'a');
doc(doc<'a')=char(25-doc(doc<'a')+2*'A');
end
这给出了:
> change('bBcC')
ans = yYxX
如果您希望/需要使用当前的方法,可以使用char进行char工作,但这样效率非常低,或者构建通过比较newclaim1
和newclaim
设置的字母掩码等等,防止它们改变。
或者您可以将上述想法与regexprep
一起使用(在我看来这样做有点过分,因为在这种情况下regexp是微不足道的):
function doc=change(doc);
doc = regexprep(doc, '([a-z])', '${char(25-($0)+2*97)}')
doc = regexprep(doc, '([A-Z])', '${char(25-($0)+2*65)}')
end
这提供了与之前相同的输出
>> change('bBcC')
ans =
yYxX