⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kgramcount.m

📁 loading a text file from a directory to your workspace in matlab
💻 M
字号:
% k-gram Counting Program
clc;
clear all;
close all;
%% Read the cyphered text from a *.txt file
text_input = loadtext('Decrypt_3.txt');
%% Remove the blanks from input text.
index = find(text_input ~= ' ');
text = text_input(index);
%% *****************************************************

len=length(text);
result=zeros(len,3);
spaces=[];

% Length of text block you are searching for repeats of
k=2;
% *****************************************************


m=k-1;
for n=1:len-m
    dig=text(n:n+m);
    for j=n+1:len-m
        if text(j:j+m)==dig
            space=j-n;
            spaces=[spaces space];
        end
    end
end

for guess=2:len
    result(guess,1)=guess; 
    result(guess,2)=length(find(mod(spaces,guess)==0));
    result(guess,3)=length(find(mod(spaces,guess)~=0));
end

% The first result column gives the period length guess
% The second result column gives the number of repeats that evenly divide the guess
% The third result column gives the number of repeats that don't evenly divide the guess

% Large values in column 2 indicate that the corresponding guess divides the actual period
results=result(2:end,:)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -