필터 지우기
필터 지우기

Converting arrayfun to loop

조회 수: 1 (최근 30일)
Zachary Holmes
Zachary Holmes 2016년 9월 7일
답변: Walter Roberson 2016년 9월 8일
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