dedbi反转这样的词,'a'将被'z'取代,'b'将被'y'取代,'c'将被'x'取代,依此类推。 dedbi将对大写字母执行相同的操作,即将字符串'A'替换为'Z','B'替换为'Y','C'替换为'X',依此类推。如果我给函数这个字符串'ab AB'函数应该返回'zy ZY',输入除了英文单词之外它将输入作为输出输入为'/// \?'将返回输出为'/// \? ”。
到目前为止,我编写了这段代码。我必须承认这个问题来自我需要传递的任务。 谢谢你们的亲切外表。
function xtxx = dedbi(xtx)
txtt = char(xtx);
indexa = strfind(txtt,'a');
txtt(indexa) = 'z';
indA = strfind(txtt,'A');
txtt(indA) = 'Z';
indb = strfind(txtt,'b');
txtt(indb) = 'y';
indB = strfind(txtt,'B');
txtt(indB) = 'Y';
indc = strfind(txtt,'c');
txtt(indc) = 'x';
indC = strfind(txtt,'C');
txtt(indC) = 'X';
indd = strfind(txtt,'d');
txtt(indd) = 'w';
indD = strfind(txtt,'D');
txtt(indD) = 'W';
inde = strfind(txtt,'e');
txtt(inde) = 'v';
indE = strfind(txtt,'E');
txtt(indE) = 'V';
indf = strfind(txtt,'f');
txtt(indf) = 'u';
indF = strfind(txtt,'F');
txtt(indF) = 'U';
indg = strfind(txtt,'g');
txtt(indg) = 't';
indG = strfind(txtt,'G');
txtt(indG) = 'T';
indh = strfind(txtt,'h');
txtt(indh) = 's';
indH = strfind(txtt,'H');
txtt(indH) = 'S';
indi = strfind(txtt,'i');
txtt(indi) = 'r';
indI = strfind(txtt,'I');
txtt(indI) = 'R';
indj = strfind(txtt,'j');
txtt(indj) = 'q';
indJ = strfind(txtt,'J');
txtt(indJ) = 'Q';
indk = strfind(txtt,'k');
txtt(indk) = 'p';
indK = strfind(txtt,'K');
txtt(indK) = 'P';
indl = strfind(txtt,'l');
txtt(indl) = 'o';
indL = strfind(txtt,'L');
txtt(indL) = 'O';
indm = strfind(txtt,'m');
txtt(indm) = 'n';
indM = strfind(txtt,'M');
txtt(indM) = 'N';
indn = strfind(txtt,'n');
txtt(indn) = 'm';
indN = strfind(txtt,'N');
txtt(indN) = 'M';
indo = strfind(txtt,'o');
txtt(indo) = 'l';
indO = strfind(txtt,'O');
txtt(indO) = 'L';
indp = strfind(txtt,'p');
txtt(indp) = 'k';
indP = strfind(txtt,'P');
txtt(indP) = 'K';
indq = strfind(txtt,'q');
txtt(indq) = 'j';
indQ = strfind(txtt,'Q');
txtt(indQ) = 'J';
indr = strfind(txtt,'r');
txtt(indr) = 'i';
indR = strfind(txtt,'R');
txtt(indR) = 'I';
inds = strfind(txtt,'s');
txtt(inds) = 'h';
indS = strfind(txtt,'S');
txtt(indS) = 'H';
indt = strfind(txtt,'t');
txtt(indt) = 'g';
indT = strfind(txtt,'T');
txtt(indT) = 'G';
indu = strfind(txtt,'u');
txtt(indu) = 'f';
indU = strfind(txtt,'U');
txtt(indU) = 'F';
indv = strfind(txtt,'v');
indv(txtt) = 'e';
indV = strfind(txtt,'V');
txtt(indV) = 'E';
indw = strfind(txtt,'w');
txtt(indw) = 'd';
indW = strfind(txtt,'W');
txtt(indW) = 'D';
indx = strfind(txtt,'x');
txtt(indx) = 'c';
indX = strfind(txtt,'X');
txtt(indX) = 'C';
indy = strfind(txtt,'y');
txtt(indy) = 'b';
indY = strfind(txtt,'Y');
txtt(indY) = 'B';
indz = strfind(txtt,'z');
txtt(indz) = 'a';
indZ = strfind(txtt,'Z');
txtt(indZ) = 'A';
str = xtxx;
end
答案 0 :(得分:0)
这是一种方法:
alphabet = 'abcdefghijklmopqrstuvwxyz';
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
ralphabet = fliplr(alphabet);
RALPHABET = fliplr(ALPHABET);
txt = 'abc d ?!.< n AC';
[~, index] = ismember(txt,alphabet);
target = index ~= 0;
src = index(target);
[~, INDEX] = ismember(txt,ALPHABET);
TARGET = INDEX ~= 0;
SRC = INDEX(TARGET);
txtInvert = txt;
txtInvert(target) = ralphabet(src);
txtInvert(TARGET) = RALPHABET(SRC);
输出:
zyx w?!。&lt; n ZX
答案 1 :(得分:0)
一种方法 -
%// in_str: Input string
%// Logical masks for upper and lower case letters
upper_mask = in_str>=65 & in_str<=90 %// OR isstrprop(in_str,'upper')
lower_mask = in_str>=97 & in_str<=122 %// OR isstrprop(in_str,'lower')
%// Initialize output string
out_str = in_str
%// Replace upper letters with "flipped" upper letters; repeat for small letters
out_str(upper_mask) = char(in_str(upper_mask) + 2*(78-in_str(upper_mask))-1)
out_str(lower_mask) = char(in_str(lower_mask) + 2*(110-in_str(lower_mask))-1)
这些数字:78
和110
充当&#34;中间数字&#34;分别用于大写和小写字母范围,用于查找这两个类别中每个字母的差异。
示例运行 -
>> in_str,out_str
in_str =
adzC+*6AY&‘///\?abAB
out_str =
zwaX+*6ZB&‘///\?zyZY
答案 2 :(得分:0)
我刚刚发布了解决方案,但我意识到我可能不应该解决你的任务。无论如何,这里有几个提示:
您可以使用函数fliplr()
反转(行)向量中的元素。 (提示:反转字母表)
如果你这样做:[~, index] = ismember('abcd','ac')
,则index
将是:index=[1 3]
(提示:字母表中的'a'和c
的索引是与反向字母表中的z
和'y'相同)
答案 3 :(得分:0)
伪代码:
function out = func_name(in)
in = array of ascii values from original in values (can be done with one command)
out = vector if zeros the size of in (one command)
loop through all numbers of in
ch = current char ascii value (just because it is easier to write ch than in(index) in my opinion)
if ch is btw 65 and 96
subtract 65 so ch is 1-26
find 26-ch (invert)
add 65 so ch is 65-96
if ch is btw 97 and 122
do the same as above with adjusted numbers
end if
out(index_of_for_loop) = ch
end loop
end function
注意:一般情况下,最好避免使用包含许多复制粘贴部分的代码,因为它很难快速编辑。
答案 4 :(得分:0)
另一种解决方案:
void blur3x3(int i, int j, RGBApixel** pixelArray, BMP &Image) {
double blurValue = 0.111;
int avgR = 0;
int avgG= 0 ;
int avgB = 0;
int b = 0;
for(int w = i-1 ; w <= i+1 ; w++) {
for(int z = i-1 ; z<=j+1 ; z++) {
avgR = avgR + blurValue*( pixelArray[w][z].Red );
avgG = avgG + blurValue*( pixelArray[w][z].Green );
avgB = avgB + blurValue*( pixelArray[w][z].Blue );
}
}
Image(i,j)->Red = (BYTE) avgR ;
Image(i,j)->Green = (BYTE) avgG;
Image(i,j)->Blue = (BYTE) avgB;
}
bool blur() {
BMP Image;
Image.ReadFromFile(fullFilePath);
int w = Image.TellWidth();
int h = Image.TellHeight();
RGBApixel** pixelArray = new RGBApixel*[w];
for(int i = 0; i < w; ++i)
pixelArray[i] = new RGBApixel[h];
for( int i=0 ; i < Image.TellWidth() ; i++) {
for( int j=0 ; j < Image.TellHeight() ; j++) {
pixelArray[i][j] = Image.GetPixel(i,j);
}
}
for( int i=1 ; i < Image.TellWidth()-1 ; i++) {
for( int j=1 ; j < Image.TellHeight()-1 ; j++) {
blur3x3(i,j, pixelArray, Image);
}
}
Image.SetBitDepth( 32 );
Image.WriteToFile( "/home/kxyz/BMP/gray.bmp" );
}