Need suggestion on Code conversion to Matlab_extension 1

后端 未结 1 1516
孤城傲影
孤城傲影 2021-01-21 14:15

This is an extension of the previously asked question: link. In a short, I am trying to convert a C program into Matlab and looking for your suggestion to improve the code as th

相关标签:
1条回答
  • 2021-01-21 15:06

    EDIT:

    Try this it do the same think as the c function: The lengh of S should be >= 256 or you will exceed it as mod() could return such index. I suggest you to change 256 in the function with the lenght provided to solve this issue.

    Here you don't need the key variable.

    function out = prga(S, len)
        i=0; j=0; x=[]; t=[];
        for x=0:len-1
            i = mod(i+1, 256);
            j = mod(j+S(i+1), 256);
            t = S(i+1);
            S(i+1) = S(j+1);
            S(j+1) = t;
        out(x+1) = S(mod(S(i+1)+S(j+1), 256)+1);
        end
    end
    

    Or you can use the key variable to controle the loop

    function out = prga(S, key)
        i=0; j=0; x=[]; t=[];
        for x=0:length(key)-1
            i = mod(i+1, 256);
            j = mod(j+S(i+1), 256);
            t = S(i+1);
            S(i+1) = S(j+1);
            S(j+1) = t;
        out(x+1) = S(mod(S(i+1)+S(j+1), 256)+1);
        end
    end
    
    0 讨论(0)
提交回复
热议问题