Filter löschen
Filter löschen

Converting arrayfun to loop

1 Ansicht (letzte 30 Tage)
Zachary Holmes
Zachary Holmes am 7 Sep. 2016
Beantwortet: Walter Roberson am 8 Sep. 2016
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 Kommentar
Walter Roberson
Walter Roberson am 8 Sep. 2016
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 8 Sep. 2016
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.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by