【问题标题】:How can i write a MATLAB function named dedbi that takes input xtx as string and returns another string xtxx as output.我如何编写一个名为 dedbi 的 MATLAB 函数,它将输入 xtx 作为字符串并返回另一个字符串 xtxx 作为输出。
【发布时间】:2015-05-25 04:19:45
【问题描述】:

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

【问题讨论】:

    标签: matlab


    【解决方案1】:

    一种方法 -

    %// 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)
    

    这些数字:78110 分别充当大写和小写字母范围的“中间数字”,用于查找这两个类别中每个字母的差异。

    示例运行 -

    >> in_str,out_str
    in_str =
    adzC+*6AY&‘///\?abAB
    out_str =
    zwaX+*6ZB&‘///\?zyZY
    

    【讨论】:

    • 谢谢。我想我必须努力理解你的代码几个小时,因为我是 MATLAB 和编程的新手。
    • @DumbCoder 很多都涉及到处理 ascii 等价物,在这里查看它的表格列表:asciitable.com
    • 谢谢你兄弟,你的提示帮助解决了这个问题,但我认为我需要花更多的时间来理解你的代码。这是你第二次在这个平台上帮助我。
    【解决方案2】:

    我刚刚发布了解决方案,但我意识到我可能不应该解决您的任务。无论如何,这里有几个提示给你:

    1. 您可以使用函数fliplr() 反转(行)向量中的元素。 (提示:颠倒字母表)

    2. 如果你这样做:[~, index] = ismember('abcd','ac'),那么index 将是:index=[1 3](提示:字母表中'a'和c的索引与z和'相同y' 在反向字母表中)

    【讨论】:

    • 谢谢兄弟的提示。作为 MATLAB 和编程的新手,我认为我需要更清楚地理解和思考这个问题。你的提示肯定会对我有所帮助。
    【解决方案3】:

    伪代码:

    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
    

    注意:一般来说,最好避免有很多复制粘贴部分的代码,因为它可能难以快速编辑。

    【讨论】:

    • 谢谢你的代码。由于我是第一次学习 ASCII,我认为您的代码需要在第一个 IF 语句中进行一些更正。我想应该是65到90之间吧?另一个问题是因为我的函数需要字符串,所以我为什么不使用 func_name('in')?还是我应该输入第二行代码,例如 in = double('in')?
    • 您必须进行大量编辑,因为这是伪代码。它只是给出了如何解决这个问题的大致思路。
    • 'out = vector if zeros the size of in (one command)' 没有得到足够的这一行。请解释一下。
    • 抱歉,我花了一段时间才看到这个。在 MATLAB 中,您通常希望在使用数组之前使它们具有正确的大小。该行应该创建一个数组用作输出。该数组将是一个由零组成的 n×1 数组。有一个内置命令可以执行此操作,语法(我认为)对于 n×m 数组是 zeros(n,m),对于 n×n 数组是 zeros(n)。
    【解决方案4】:

    这是一种方法:

    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 ?!.

    【讨论】:

    • 谢谢。请提供一些链接来理解在 [] 中使用 ~。我真的很困惑。
    • 如果matlab在函数返回多个参数时可以使用[par1, par2, ... parN] = myFunction()默认情况下,如果你说par = myFunction(),则只返回第一个。当你只想要一个返回的参数不是第一个时,你可以说[~, par],在这种情况下第一个返回参数被忽略。
    【解决方案5】:

    另一种解决方案:

    function xtx = dedbi(xtx)
        xtx(xtx >= 'a' & xtx <= 'z') = 219 - xtx(xtx >= 'a' & xtx <= 'z');
        xtx(xtx >= 'A' & xtx <= 'Z') = 155 - xtx(xtx >= 'A' & xtx <= 'Z');
    end
    

    【讨论】:

      猜你喜欢
      • 2018-05-23
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 2021-11-09
      相关资源
      最近更新 更多