digraph.m

来自「有趣的可视的数值方法 出自网站http://www.mathworks.com」· M 代码 · 共 41 行

M
41
字号
function digraph(file)%DIGRAPH  Principal component analysis of text digraph frequency matrix.%  DIGRAPH(filename)%  Example%     digraph('gettysburg.txt')% Read entire filefid = fopen(file);txt = fread(fid);fclose(fid);% Convert to integers between 1 and 26k = upper(char(txt)) - 'A' + 1;k(k < 1 | k > 26) = [];% Generate the digraph frequency matrix and compute its SVDj = k([2:length(k) 1]);A = full(sparse(k,j,1,26,26));cnt = sum(A);[U,S,V] = svd(A);% Plot the second left and right singular vectorsshgclfs = 4/3*max(max(abs(U(:,2))),max(abs(V(:,2))));axis(s*[-1 1 -1 1])axis squarefor i = 1:26   if cnt(i) > 0      text(U(i,2),V(i,2),char('A'+i-1))   endendline([0 0],[-s s],'color','b')line([-s s],[0 0],'color','b')boxtitle(sprintf('%d characters',length(k)))

⌨️ 快捷键说明

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