Converting arrayfun to loop
1 回表示 (過去 30 日間)
古いコメントを表示
So in this code,
function cipher_text = vigenere_cipher(origionalText,key)
Array = Operator;
key = lower(key) - double('a') + 1; %Converts all text to lowercase
key(key < 0) = 27;
origionalText = lower(origionalText) - double('a') + 1;
origionalText(origionalText < 0) = 27;
keyLength = rem(0:(numel(origionalText)-1), numel(key))+1; %Converst the key to the length of the origional text
k = key(keyLength);
% Encrypt: C(n) = V(k(n), plaintext(n))
cipher_text = arrayfun(@(m,n) Array(m,n), k, origionalText) - 1;
cipher_text(cipher_text == 26) = double(' ') - double('a');
cipher_text = upper(char(cipher_text + double('a')));
end
i want to change:
cipher_text = arrayfun(@(m,n) Array(m,n), k, origionalText) - 1;
cipher_text(cipher_text == 26) = double(' ') - double('a');
cipher_text = upper(char(cipher_text + double('a')));
to a for loop. How would i do that?
1 件のコメント
Walter Roberson
2016 年 9 月 8 日
How does this differ from http://www.mathworks.com/matlabcentral/answers/302182-how-to-convert-arrayfun-to-for-loop#comment_389765 in which you said that you got the code working, in response to someone else who asked about converting the same code to a for loop?
回答 (1 件)
Walter Roberson
2016 年 9 月 8 日
A = arrayfun( @(p,q) B(m,n), C, D);
can be converted to
A = zeros(size(C));
for idx = 1 : numel(C)
A(idx) = B( C(idx), D(idx) );
end
Except that this code does not correctly account for the possibility that a data type other than double is being returned.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!