Converting arrayfun to loop

1 次查看(过去 30 天)
Zachary Holmes
Zachary Holmes 2016-9-7
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
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
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by