필터 지우기
필터 지우기

How to convert arrayfun to for loop

조회 수: 1 (최근 30일)
Matt Holm
Matt Holm 2016년 9월 7일
편집: Andrei Bobrov 2016년 9월 8일
I found a source code online for a vigenere cipher and was wondering how to convert it to a for loop.
function Array = Operator
count = 27; %Primary function of the vignere encryptions by creating a 27x27 array
Length = 1:count; %array of length 27
% ie, Create a matrix with 27 shifted substitution alphabets
% 1 2 3 4 5 ... 26 27
% 2 3 4 5 6 ... 27 1
% 3 4 5 6 7 ... 1 2
% etc.
Array = arrayfun(@(n) circshift(Length, [0, -n]), 0:count-1, ...
'UniformOutput', false);
Array = reshape([Array{:}], count, count);
end
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

채택된 답변

Thorsten
Thorsten 2016년 9월 7일
count = 27;
Length = 1:count;
for n = 0:count-1
Array(n+1,:) = circshift(Length, [0, -n]);
end
  댓글 수: 5
Zachary Holmes
Zachary Holmes 2016년 9월 7일
All good. i got it working thanks!!
Thorsten
Thorsten 2016년 9월 8일
So if you're happy, please accept my answer.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2016년 9월 7일
편집: Andrei Bobrov 2016년 9월 8일
Array = hankel(1:27,[27,1:26]);
or
count = 27;
L = (1:count)';
Array1 = zeros(count);
for jj = 1:count
Array1(jj,:) = rem(L+jj-2,count)+1;
end
or
Array = hankel(L,circshift(L,[0,1]));
  댓글 수: 2
Zachary Holmes
Zachary Holmes 2016년 9월 7일
I don't want to create a new function, i just want to rewrite
Array = arrayfun(@(n) circshift(Length, [0, -n]), 0:count-1, ... 'UniformOutput', false); Array = reshape([Array{:}], count, count);
that bit of code differently
Andrei Bobrov
Andrei Bobrov 2016년 9월 7일
:)
Array = hankel(1:27,[27,1:26]);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by