目录

1. 图片转为ASCII字符

2. 生成迷宫

3. 生成词云图


1. 图片转为ASCII字符

这个比较有意思,可以将一个图像转换为ASCII文字,具体工具箱代码如下:

function img2txt(imfile,varargin);
%IMG2TXT Converts an image to ASCII text
%
%   img2txt(imfile) converts the image contained in the specified file
%                   using an ASCII character for every pixel in x-dimension
%   img2txt(imfile,stepx) converts the image contained in the specified file
%                   using an ASCII character for every stepx pixels in x-dimension
%%   Copyright (c) by Federico Forte
%   Date:     2004/04/08
%   Revision: 2004/04/27 ramp=['@@@@@@@######MMMBBHHHAAAA&&GGhh9933XXX222255SSSiiiissssrrrrrrr;;;;;;;;:::::::,,,,,,,........'];% the 'ramp' vector represents characters in order of intensity
im=imread(imfile);
im=mean(im,3);
fid=fopen(strcat([imfile,'.txt']),'w');
stepx=1;
if length(varargin)>0,stepx=varargin{1};
end
stepy=2*stepx;
sizx=fix(size(im,2)/stepx);
sizy=fix(size(im,1)/stepy);
lumin=zeros(sizy,sizx);
for j=1:stepy,for k=1:stepx,lumin=lumin+im(j:stepy:(sizy-1)*stepy+j,k:stepx:(sizx-1)*stepx+k);end
end
str=ramp(fix(lumin/(stepx*stepy)/256*length(ramp))+1);
for h=1:sizy,fwrite(fid,[str(h,:),13,10]);
end
fclose(fid);

例如,当我们需要将下面这张图像转换为Ascii字符串,那么可以这样:

img2txt('callmejack.jpg')

最终效果如下所示:

......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
............................................................,,,,,,,,::::;;;;;;;;;;;;:,,,..............................................................
..........................................................,::;rsi52SS555555SS93X2i;;;rr;;:,...........................................................
.....................................................:;:,:;iXAAAAA2siiiiiiiiiAAAAr;;;;;;rrr;:,........................................................
.................................................:i3Hi::;2hAAAAAA9ssiiiiiiii9AAAi;;;;;;i3Srsr;;,,.....................................................
..............................................,s&MHi::r2&AAAAAAA&isiiiiiiii5AAA5;;;;;;r&A9i5rrrr;:,,..................................................
............................................:2MMH2::;2AAAAAAAAA&issiiiiiiiiGAAS;;;;;;;9Ahi2AA2rrrr;,,.................................................
..........................................,2MMM9;,;XAAAAAAAAAA&issiiiiiiii9AAS;;rr;;;3A9i2AAAA&irrr,,.................................................
.........................................;AMMMHr;,:GAAAAAAAAA&SssiiiiiiiiXGs;:::::;r3A3i3AAAAAAA5rr,,r................................................
........................................;MMMMASSr;,;AAAAAAAA&issiiiiiiii5i::ri522Sr;:iX&AAAAAAAAA5r,,i,...............................................
.......................................;MMMMB252sr:,rAAAAAAASssiiiiiii5r:;r9MMMMMMHXr;,;i&AAAAAAAAs,,i,...............................................
......................................,BMMMHHhS52rr:,SAAAAA2sssiiiii5r:;s9MMMMMMMMMMBXr:,::::::::::,:S,...............................................
......................................2MMMAAAH25S5r;,,SAAA&Sssssss;;:;iGMMMMMBBBBBBMMMH2irr;;;;;;;;r2r................................................
......................................HMMHAAAAA55iir;,,::::;;;;;ri5223AAAAAAAAAAAAHAAAAAAAHHHHAABMM2,.................................................
.....................................,MMHAAAAAA&2S2SisiiS22XX9GAAAAHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHXr:................................................
.....................................rMMAAAAAAAAHABMMMBBHAHHHHHHHHHHHHHAh3X22555SSS52XGG933XXXXX3hHHHHX...............................................
.....................................rMHAAAAAAAB#MMMMAHHAA&GG93333X2ir;;:,,,,,,,,;;s2229&AAAAAAA99HHXr,...............................................
.....................................,BHHAAAAHM#MMMMMh333G&G&AAAAA&&GGh9X325iii23h&AHBMMMMMMMMMMXi;,..................................................
......................................AHHHAAHMMMMMMMMh33X2SXXAABMMMMMMMMMMMA33XGH9223X9HAABMMMMM......................................................
.....................................;HHHHHHMMMM###MMA39MAH3X522AGHMMMMMMMMA3332&HHBAX3X222&Ah&M......................................................
.....................................;HHHHHB#MMM###MM2rHMMMMHAhX52Xh&ABMMMA32;;559MMMMBHHAhX223i......................................................
......................................;AHHBMMMMMMMMMMr;MMMMMMMMMAA3SSXX9AAh2:,,,S2BMMMMMMMMM2s,,.,:;;rr:..............................................
.......................................:GHMMMMMMMMMMM:,rSHMMMMMMMMMHAGSSSXi:,,,,,;XMMMAXir;,,,,,:;:,..................................................
........................................r&H##MM9&MMMH;,,,,:;s23&AHHHHHA9i:,,:rs;,,,,,,,,,,,,,,,,,::::,,...............................................
........................................r;;MMMMG332AAi,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,:,,,,,,,,,,,,..,:;;r:.........,...................................
...........................................SMMMAGX52SSr,,,,,,,,,,,,,,SSr:,,,,,,,:;rs,,,,,,,,,,,,.............,...,....................................
........................................:sir:,.;2hSiS3r,,,,,,,,,,,,,,:3GGh93395iiis:,,,,,,,,,,,.............,....,....................................
........................................;,.....:;iiGGA2,,,,,,,,,,,,,,,,rssi5292iir,,,,,,,,,:rs..............:....,....................................
............................................:r5r:..:s&As,,,,,,,,,,,,,,,,:;rrrsi;:,,,,,,,,.,siS:.............,.........................................
..........................................,sS;.......,5iSsr;:,,,,,,,,,,,,,,:;rrr;;;;:,.....,sr,......::,,::,;,,,,,,,..................................
..........................................:r..........siSi:;rsrrr;;;;;;;rr2333Xi;,................,:;,:r2GHH&2ir;:,:;:................................
......................................................r5r:..........,,,,riir22iSs;..............:,.,r:,,,,::;;rrr;::,,:;:.............................
....................................................................,rsiiSi;riSisSs;...........:;...;:,,,:;::,,,,,,,,:SSSi;...........................
.................................................................,:riiiiiSii,;S5;SSir:.........,;...;;,,,i####AB::;rs25SSSSi..........................
..............................................................,;riiiiiiiiiiSsrSSriisSir:........,:,.,r:r;GXi23G9iSSSSSSSSSSS:.........................
...........................................................,;siiiiiiiii2SiiiS25s22ii5Siis;,.......,:;sr:rS32SSSSSSSSSSSSSSSS:.........................
........................................................,:siiiiiiiiiSr,2iiiiSSs25siiSXSiiiir:.......;rsr;23SSSSSSSSSSSSSSSSi..........................
......................................................:riiiiiiiiii5s,.;99922XG3SiX3S3&&9iiiiis;,...:2;;irsSX5SSSSSSSSSSSSS2;..........................
....................................................:riiiiiiiiii55:..,HBBHHHHH3S3GGXAHHH2iiiiiSSisiSii::;rs522SSSSSSSSSS2X;...........................
..................................................:siiiiiiiiiiS2s....rMMBHHh9hX2X395&BBBX2iiiiSiSSiiiir,,,r2333X2222222i;.............................
.................................................riiiiiiiiiiiSX;.....rHHAAH9222SS53GAHAs;2X2iiiiiiiiiiis:,,rrsrrr::::,................................
................................................:XiiiiiiiiiiiiSr,....;BHHHHHHHHHHAHHHHH;..;5X2Siiiiiiiiiirrrrrsi......................................
................................................;Xiiiiiiiiiiiiiiis;,2AHHHHHHHHHHHHHHHHAAS...:rSXX55SSiiiS5Sii22;......................................
................................................,X2iiiiiiiiiiiiiiiiiS3HBHHHHHHHHHHHHHHHH;.......:;;rsS2225ir;:........................................
.................................................:22SiiiiiiiiiiiiiiiiiS3AHHHHHHHBHHHHHHH:.............................................................
..................................................,59SiiiiiiiiiiSis;:::;sAHHHHHHBHHHHHHH;.............................................................
....................................................r3X5Siiiii5i;,,,,,,,,,sAHHHHBHHHHHHHh.............................................................
.....................................................,sX9XiiSS;,,,,,,,,,,,,:&HHHBBHHHHHHH.............................................................
.......................................................:SXXXSrr,,,,::;rsi2X5hHHHBBHHHHHHH.............................................................
.....................................................:sSSSS5isrr;rsSSSSS233AHHHHBBHHHHHHH;............................................................
.....................................................;52SSSSSSisiSSSSSSX33ABHHHHBBHHHHHHAr,...........................................................
.......................................................:525SSSSSSSS52X33GBBBHHHH9h&9X25r;;,...........................................................
.........................................................;iXXXXXXX33X22h&AAAA&G2;;i;;;;;r:............................................................
............................................................:22555SSiiir;;;;;;;;rrirrrrr;,............................................................
............................................................;SS5h&5srr;;;;;;;;;;;;SSSSS2r;:...........................................................
.........................................................;5HMA&AB##Hissrrrrrsi59&MBHAAHHHHHH3s........................................................
.....................................................:i&##MBBBM######HAAAAHB#############MMBBMA,......................................................
....................................................;###MHHM########BHHHHHM##MM################i,.....................................................
..............................................,::rri###MHHB##########MBHHHH###M#####H99999993X5iS;:::,................................................
..........................................,:;;siiiS2AMMHHHHA###########MHB####3G3XX2SSSSSSSSS52X&i;;;;;:,.............................................
.........................................:;;;siiiiSSSSSSSSSSGABM#######MHA&&A2X&AAABBMMMMM#####M9r;;;;;;;:............................................
.........................................,;;;;rrii9AAA&GG&GSSSSSSS555SiiiiSSXAirrrrsssSS2X2SSsr;;;;;;;;;;:............................................
...........................................,:;;;;;;rsiiXGG9&GX25SSSSSSSSS29Ahs;;;;;;;;;;;;;;;;;;;;;;;;:,..............................................
...............................................,:::;;;;;rsiSXXhAHAGGGhGAA32s;;;;;;;;;;;;;;;;;;;;:::,..................................................
.......................................................,,,,,::;;;rrrr;;;;;;;;;;;;;;::::,,,,,,.........................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................

2. 生成迷宫

这是MathWorks上一位父亲为了让自己的女儿玩迷宫游戏而开发的工具箱,满满的父爱,源码如下:

function thyme = maze(row,col,pattern)
% usage  thyme = maze(30,45,'c');
% row - number of rows in the maze
% col - number of column in the maze
% pattern - random(r), vertical(v), horizontal(h), checkerboard(c), spiral(s), burst(b)% Written by Rodney Meyer
% rodney_meyer@yahoo.com
%
% Construct graph system for maze. The graph entities are an id for each
% intersection(id), the physical row(rr) and column(cc) of the
% intersection, membership to a connected region (state), and a link to
% adjacent intersections(ptr_up ptr_down ptr_left ptr_right).
% Prior to "make_pattern" the maze has all of the walls intact and
% there are row*col of unconnected states. After "make_pattern" some of the
% walls are broken down and there is only one connected state for the maze.
% A broken wall(allowed passage) in some direction is signified by a negative
% value of the pointer in that direction. A solid wall(unallowed passage)
% in some direction is signified by a positive value of the pointer in that
% direction. The absolute value of the pointer is the id of the
% intersection in that direction.rand('state',sum(100*clock))[cc,rr]=meshgrid(1:col,1:row);
state = reshape([1:row*col],row,col); % state identifies connected regions
id = reshape([1:row*col],row,col); % id identifies intersections of maze% create pointers to adjacent intersections
ptr_left = zeros(size(id));
ptr_up = zeros(size(id));
ptr_right = zeros(size(id));
ptr_down = zeros(size(id));ptr_left(:,2:size(id,2)) = id(:,1:size(id,2)-1);
ptr_up(2:size(id,1),:) = id(1:size(id,1)-1,:);
ptr_right(:,1:size(id,2)-1) = id(:,2:size(id,2));
ptr_down(1:size(id,1)-1,:) = id(2:size(id,1),:);% sort graph entities by id
the_maze = cat(2,reshape(id,row*col,1),reshape(rr,row*col,1),reshape(cc,row*col,1),reshape(state,row*col,1),...reshape(ptr_left,row*col,1),reshape(ptr_up,row*col,1),reshape(ptr_right,row*col,1),reshape(ptr_down,row*col,1)  );the_maze = sortrows(the_maze);id=the_maze(:,1);
rr=the_maze(:,2);
cc=the_maze(:,3);
state=the_maze(:,4);
ptr_left=the_maze(:,5);
ptr_up=the_maze(:,6);
ptr_right=the_maze(:,7);
ptr_down=the_maze(:,8);
clear the_maze;% create a random maze
[state, ptr_left, ptr_up, ptr_right, ptr_down]=...make_pattern(row,col,pattern,id, rr, cc, state, ptr_left, ptr_up, ptr_right, ptr_down);% show maze
h=figure('KeyPressFcn',@move_spot,'color','white');
show_maze(row, col, rr, cc, ptr_left, ptr_up, ptr_right, ptr_down,h);% start play
cursor_pos=[1,1];
current_id=1;
figure(h)
text(cursor_pos(1),cursor_pos(2),'\diamondsuit','HorizontalAlignment','Center','color','r');
set(gcf,'Units','normalized');
set(gcf,'position',[0 0 1 .91]);
tic% keep processing keystrokes until the maze is solved
while ~all(cursor_pos == [col,row])waitfor(gcf,'CurrentCharacter')set(gcf,'CurrentCharacter','~') % update to another character so repeats are recognized% key is updated by move_spotswitch double(key(1))case 108 % leftif ptr_left(current_id)<0 % check for legal movecurrent_id=-ptr_left(current_id);text(cursor_pos(1),cursor_pos(2),'\diamondsuit','HorizontalAlignment','Center','color',[.8,.8,.8]);cursor_pos(1)=cursor_pos(1)-1;text(cursor_pos(1),cursor_pos(2),'\diamondsuit','HorizontalAlignment','Center','color','r');endcase 114 % rightif ptr_right(current_id)<0 % check for legal movecurrent_id=-ptr_right(current_id);text(cursor_pos(1),cursor_pos(2),'\diamondsuit','HorizontalAlignment','Center','color',[.8,.8,.8]);cursor_pos(1)=cursor_pos(1)+1;text(cursor_pos(1),cursor_pos(2),'\diamondsuit','HorizontalAlignment','Center','color','r');endcase 117 % upif ptr_up(current_id)<0 % check for legal movecurrent_id=-ptr_up(current_id);text(cursor_pos(1),cursor_pos(2),'\diamondsuit','HorizontalAlignment','Center','color',[.8,.8,.8]);cursor_pos(2)=cursor_pos(2)-1;text(cursor_pos(1),cursor_pos(2),'\diamondsuit','HorizontalAlignment','Center','color','r');endcase 100 % downif ptr_down(current_id)<0 % check for legal movecurrent_id=-ptr_down(current_id);text(cursor_pos(1),cursor_pos(2),'\diamondsuit','HorizontalAlignment','Center','color',[.8,.8,.8]);cursor_pos(2)=cursor_pos(2)+1;text(cursor_pos(1),cursor_pos(2),'\diamondsuit','HorizontalAlignment','Center','color','r');endotherwiseendendthyme=toc;
title(cat(2,' Winning Time ',num2str(round(thyme*100)/100),'(sec)'),'FontSize',20)
returnfunction move_spot(src,evnt)
assignin('caller','key',evnt.Key)
returnfunction show_maze(row, col, rr, cc, ptr_left, ptr_up, ptr_right, ptr_down,h)
figure(h)
line([.5,col+.5],[.5,.5]) % draw top border
line([.5,col+.5],[row+.5,row+.5]) % draw bottom border
line([.5,.5],[1.5,row+.5]) % draw left border
line([col+.5,col+.5],[.5,row-.5])  % draw right border
for ii=1:length(ptr_right)if ptr_right(ii)>0 % right passage blockedline([cc(ii)+.5,cc(ii)+.5],[rr(ii)-.5,rr(ii)+.5]);hold onendif ptr_down(ii)>0 % down passage blockedline([cc(ii)-.5,cc(ii)+.5],[rr(ii)+.5,rr(ii)+.5]);hold onendend
axis equal
axis([.5,col+.5,.5,row+.5])
axis off
set(gca,'YDir','reverse')
returnfunction [state, ptr_left, ptr_up, ptr_right, ptr_down]=make_pattern(row,col,pattern,id, rr, cc, state, ptr_left, ptr_up, ptr_right, ptr_down)while max(state)>1 % remove walls until there is one simply connected regiontid=ceil(col*row*rand(15,1)); % get a set of temporary ID'scityblock=cc(tid)+rr(tid); % get distance from the startis_linked=(state(tid)==1); % The start state is in region 1 - see if they are linked to the starttemp = sortrows(cat(2,tid,cityblock,is_linked),[3,2]); % sort id's by start-link and distancetid = temp(1,1); % get the id of the closest unlinked intersection% The pattern is created by selective random removal of vertical or % horizontal walls as a function of position in the maze. I find the% checkerboard option the most challenging. Other patterns can be addedswitch upper(pattern) case 'C' % checkerboarddir = ceil(8*rand);nb=3;block_size =  min([row,col])/nb;while block_size>12nb=nb+2;block_size =  min([row,col])/nb;endodd_even = (ceil(rr(tid)/block_size)*ceil(col/block_size) + ceil(cc(tid)/block_size));if odd_even/2 == floor(odd_even/2)if dir>6dir=4;endif dir>4dir=3;endelseif dir>6dir=2;endif dir>4dir=1;endendcase 'B' % burstdir = ceil(8*rand);if abs((rr(tid)-row/2))<abs((cc(tid)-col/2))if dir>6dir=4;endif dir>4dir=3;endelseif dir>6dir=2;endif dir>4dir=1;endendcase 'S' %spiraldir = ceil(8*rand);if abs((rr(tid)-row/2))>abs((cc(tid)-col/2))if dir>6dir=4;endif dir>4dir=3;endelseif dir>6dir=2;endif dir>4dir=1;endendcase 'V'dir = ceil(8*rand);if dir>6dir=4;endif dir>4dir=3;endcase 'H'dir = ceil(8*rand);if dir>6dir=2;endif dir>4dir=1;endotherwise % randomdir = ceil(4*rand);end% after a candidate for wall removal is found, the candidate must pass% two conditions. 1) it is not an external wall  2) the regions on% each side of the wall were previously unconnected. If successful the% wall is removed, the connected states are updated to the lowest of% the two states, the pointers between the connected intersections are% now negative.switch dircase -1case 1if ptr_left(tid)>0 & state(tid)~=state(ptr_left(tid))state( state==state(tid) | state==state(ptr_left(tid)) )=min([state(tid),state(ptr_left(tid))]);ptr_right(ptr_left(tid))=-ptr_right(ptr_left(tid));ptr_left(tid)=-ptr_left(tid);endcase 2if ptr_right(tid)>0 & state(tid)~=state(ptr_right(tid))state( state==state(tid) | state==state(ptr_right(tid)) )=min([state(tid),state(ptr_right(tid))]);ptr_left(ptr_right(tid))=-ptr_left(ptr_right(tid));ptr_right(tid)=-ptr_right(tid);endcase 3if ptr_up(tid)>0 & state(tid)~=state(ptr_up(tid))state( state==state(tid) | state==state(ptr_up(tid)) )=min([state(tid),state(ptr_up(tid))]);ptr_down(ptr_up(tid))=-ptr_down(ptr_up(tid));ptr_up(tid)=-ptr_up(tid);endcase 4if ptr_down(tid)>0 & state(tid)~=state(ptr_down(tid))state( state==state(tid) | state==state(ptr_down(tid)) )=min([state(tid),state(ptr_down(tid))]);ptr_up(ptr_down(tid))=-ptr_up(ptr_down(tid));ptr_down(tid)=-ptr_down(tid);endotherwisedirerror('quit')endend
return

我们该如何使用呢?

function hands_free_maze
% My daughter likes playing maze, but there were a couple of problems. She
% is only in kindergarten so invoking a function is not in her vocabulary
% yet, she gets bored if the mazes are not progressively harder, and I get
% tired of restarting maze every couple of minutes.... so here is a little
% infinite loop to keep even a kindergartener amused.rr=10;
cc=15;
ii=1;
while 1close allscore(ii,1) = maze(rr,cc,'r')pause(5)rr=round(rr*1.12);cc=round(cc*1.12);ii=ii+1;
end

3. 生成词云图

function [pos,LL]=WordCloud2_gui%% make Word Cloud
% WordCloud
% Result is Reult.mat file
% The Result file should be like following structure
% Result.Binary (Binary Data) Result.KeyWords (KeyWords)
% x_std y_std is how much you want to spread
% Max Min: Range of FontSize
% Color: Colormap(jet, hsv, spring, summer, autumn, winter, hot, cool, parula)
% all things of argumentations are compulsory here
% Ikko Kimura, 2015/09/16, Osaka University
% Ikko Kimura, 2015/09/14 add some options..(min, Color)
% I've changed the script in following URL to make this function https://github.com/toshiakit/MeCab-Win-and-MATLAB/blob/master/mecab-script.m%%%%%%%%%%%%%%%%%%%%% GUI STUFF %%%%%%%%%%%%%%%%%%%%%%%%%%%%display('Selecting the Result File to analyze...')[Name,Path]=uigetfile('*.mat','Please Select the Result file');Result=[Path,Name];display(sprintf('ResultFile Name: %s',Result))load(Result);display('Done!')prompt={'Spread (Xaxis)';'Spread (Yaxis)';'MaxFontSize';'MinFontSize';'ColorMap';'Iteration'};DefaultAns={num2str(10),num2str(10),num2str(100),num2str(30),'cool',num2str(10000)};options.Resize='on'; options.WindowStyle='normal';Answer=inputdlg(prompt,'Word Cloud',1,DefaultAns,options);x_std=str2double(Answer(1)); y_std=str2double(Answer(2));Max=str2double(Answer(3)); Min=str2double(Answer(4));Color=char(Answer(5)); Iteration=str2double(Answer(6));switch Colorcase 'hsv'colors = colormap(hsv(length(Result.KeyWords)));case 'jet'colors = colormap(jet(length(Result.KeyWords)));case 'parula'colors = colormap(parula(length(Result.KeyWords)));case 'hot'colors = colormap(hot(length(Result.KeyWords)));case 'cool'colors = colormap(cool(length(Result.KeyWords)));case 'spring'colors = colormap(spring(length(Result.KeyWords)));case 'summer'colors = colormap(summer(length(Result.KeyWords)));case 'autumn'colors = colormap(autumn(length(Result.KeyWords)));case 'winter'colors = colormap(winter(length(Result.KeyWords)));
end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alpha=0.0001; %learning parameters
Gamma=0.6;
Beta=0.6;
Delta=1.5;
lambda1=0.001;
lambda2=0.65;
% BE AWARE THAT WE USE THE PARAMETER ABOVE IN THE FOLLOWING EQUATIONS!
% Q_i=(sizes(IX(ii))/Delta)^(Beta)*((word_length(IX(ii))/max(word_length)))^Gamma;
% Q_j=(sizes(IX(jj))/Delta)^(Beta)*((word_length(IX(jj))/max(word_length)))^Gamma;
% Q=Q+(Q_i*Q_j/sqrt(dot(g,g)));
% Q_g=Q_g+g.*(Q_i*Q_j/(dot(g,g)*norm(g)));
% L(i)=Q+lambda*sum(dot(pos,pos));
% pos=pos-alpha*(PosG)-alpha*lambda*pos;Beta2=0.8; % linearly or unlinearly in the text fontsize...
%sizes =(freq)^(Beta2) * (Max-Min)+Min; %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Binary=Result.Binary;
key_words=Result.KeyWords;% check the length of each keywords
word_length=zeros(length(key_words),1);
for i=1:length(key_words)word_length(i)=length(key_words{i});clear i
endfreq=sum(Binary,1)./sum(sum(Binary));
sizes =(freq.^(Beta2)) * (Max-Min)+Min;
[~,IDX]=sort(freq,'descend');
pos=randn(length(key_words),2);
[~,order] = sort(abs(pos(:,1).*pos(:,2)));
pos = pos(order,:);
pos = pos(IDX,:);
pos(1,:)=x_std.*pos(1,:);
pos(2,:)=y_std.*pos(2,:);LL=zeros(Iteration,1); % Initializefor i=1:Iteration
L=0;
POS_G=zeros(length(key_words),2);
for ii=1:length(key_words)Pos_g=zeros(1,2);for jj=1:length(key_words)g=pos(ii,:)-pos(jj,:);if ii~=jjQ_i=(sizes(ii)/Delta)^(Beta)*((word_length(ii)/max(word_length)))^Gamma;Q_j=(sizes(jj)/Delta)^(Beta)*((word_length(jj)/max(word_length)))^Gamma;L=L+(Q_i*Q_j/(dot(g,g)));Pos_g=Pos_g+g.*(Q_i*Q_j/(dot(g,g)*sqrt(dot(g,g))));endendPOS_G(ii,:)=Pos_g;
clear ii
end
pos=pos+alpha*POS_G-alpha*(repmat((lambda1+freq'.*lambda2),1,2).*pos);
L=L+1/2*sum((lambda1+freq'.*lambda2).*(sum(pos.^2,2)));
LL(i)=L;display(sprintf('Iteration %i',i))
display(sprintf('L is now %i',L))
clear L
end
figure(1)
subplot(1,2,1)
plot(pos(:,1),pos(:,2),'b.')
subplot(1,2,2)
plot(LL,'-b')
xlabel('Iteration')
ylabel('Energy (L)')
figure(2)
xlim([-20 20]); % x axis range
ylim([-20 20]); % y axis range
hold on
for i=1:length(key_words)text(pos(i,1),pos(i,2),char(key_words{i}),'FontSize',sizes(i),'Color',colors(i,:),'HorizontalAlignment','center');
end
hold offaxis offend
%function [pos LL]=WordCloud
clear
%% make Word Cloud
% WordCloud
data= {'''book''',13;'''text''',13;'''statistics''',9;'''class''',8;'''Stat''',5;'''University''',5;'''found''',5;'''Berkeley''',4;'''course''',4;'''language''',4;'''problems''',4;'''statistical''',4;'''students''',4;'''teaches''',4;'''BA''',3;'''California''',3;'''PhD''',3;'''Statistics''',3;'''advanced''',3;'''answers''',3;'''how''',3;'''math''',3;'''number''',3;'''received''',3;'''student''',3;'''teacher''',3;'''worked''',3;'''Department''',2;'''Introductory''',2;'''US''',2;'''authors''',2;'''back''',2;'''better''',2;'''books''',2;'''chapter''',2;'''concepts''',2;'''confused''',2;'''courses''',2};
display('Done!')x_std=50;
y_std=50;
Max=7.5;
Min=2;
Iteration=500;Result.KeyWords=data;
colors = colormap(cool(length(Result.KeyWords)));%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alpha=0.01; %learning parameters
Gamma=1.5;
Beta=1.3;
Delta=0.5; % the weight of positive electric charge
lambda1=0.8; % how much you want to move words to its centre in general
lambda2=1.2; % how much you want to move the Improtant words to its centre% BE AWARE THAT WE USE THE PARAMETER ABOVE IN THE FOLLOWING EQUATIONS!
% Q_i=(sizes(IX(ii))/Delta)^(Beta)*((word_length(IX(ii))/max(word_length)))^Gamma;
% Q_j=(sizes(IX(jj))/Delta)^(Beta)*((word_length(IX(jj))/max(word_length)))^Gamma;
% Q=Q+(Q_i*Q_j/sqrt(dot(g,g)));
% Q_g=Q_g+g.*(Q_i*Q_j/(dot(g,g)*norm(g)));
% L(i)=Q+lambda*sum(dot(pos,pos));
% pos=pos-alpha*(PosG)-alpha*lambda*pos;Beta2=0.85; % linearly or unlinearly in the text fontsize...
%sizes =(freq)^(Beta2) * (Max-Min)+Min; %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Binary=Result.Binary;
%key_words=results.unique_words;
key_words=data(:,1);
for i=1:length(key_words)key=key_words{i,1};key=key(2:end-1); % stripping off the ' 'key_words{i,1}=key; clear i
end% check the length of each keywords
for i=1:length(key_words)word_length(i)=length(key_words{i});clear i
end%freq=sum(Binary,1)./sum(sum(Binary));
%freq=results.freq;
%freq=freq';
freq=cell2mat(data(:,2));
freq=freq';sizes =(freq.^(Beta2)) * (Max-Min)+Min;
pos=randn(length(key_words),2);
[~,order] = sort(abs(pos(:,1).*pos(:,2)));
pos = pos(order,:);pos(1,:)=x_std.*pos(1,:);
pos(2,:)=y_std.*pos(2,:);
for i=1:Iteration
L=0;for ii=1:length(key_words)Pos_g=zeros(1,2);for jj=1:length(key_words)g=pos(ii,:)-pos(jj,:);if ii~=jjQ_i=(sizes(ii)/Delta)^(Beta)*((word_length(ii)/max(word_length)))^Gamma;Q_j=(sizes(jj)/Delta)^(Beta)*((word_length(jj)/max(word_length)))^Gamma;L=L+(Q_i*Q_j/(dot(g,g)));Pos_g=Pos_g+g.*(Q_i*Q_j/(dot(g,g)*sqrt(dot(g,g))));endendPOS_G(ii,:)=Pos_g;
clear ii
end
pos=pos+alpha*POS_G-alpha*(repmat((lambda1+freq'.*lambda2),1,2).*pos);
L=L+1/2*sum((lambda1+freq'.*lambda2).*(sum(pos.^2,2)));
LL(i)=L;display(sprintf('Iteration %i',i))
display(sprintf('L is now %i',L))
clear L
end
figure(1)
subplot(1,2,1)
plot(pos(:,1),pos(:,2),'b.')
subplot(1,2,2)
plot(LL,'-b')
xlabel('Iteration')
ylabel('Energy (L)')
figure(2)
xlim([-30 30]); % x axis range
ylim([-30 30]); % y axis range
hold on
for i=1:length(key_words)text(pos(i,1),pos(i,2),char(key_words{i}),'FontSize',sizes(i),'Color',colors(i,:),'HorizontalAlignment','center');
end
hold offaxis off

【Matlab】ASCII图片生成、迷宫生成和词云图生成相关推荐

  1. python节日快乐_【python】一键生成漂亮的节日快乐词云图

    一键生成漂亮的节日快乐词云图,给女神一个特别的礼物~ # Python 实用宝典 # 2020/03/23 先看效果图: 代码传送门: import numpy import multidict im ...

  2. 用Wordcloud生成指定形状的词云图

    wordcloud是Python扩展库中一种将词语用图片表达出来的一种形式,通过词云生成的图片,我们可以更加直观的看出某篇文章的故事梗概. 首先贴出一张词云图(以哈利波特小说为例): 在生成词云图之前 ...

  3. [Python]*词云图生成——默认和图片蒙版词云图

    1.生成默认画布词云图 : import wordcloud as wc #导入词云库 import jieba #jieba中文分词库 import matplotlib.pyplot as plt ...

  4. python image stiching_Python自然语言处理,词云图生成

    自然语言处理 本节介绍如何使用Python中的库,生成词云图,涉及自然语言处理的相关问题,自然语言处理是计算机科学领域与人工智能领域中的一个重要方向.它研究能实现人与计算机之间用自然语言进行有效通信的 ...

  5. echarts词云图形状_用Wordcloud生成指定形状的词云图

    wordcloud是Python扩展库中一种将词语用图片表达出来的一种形式,通过词云生成的图片,我们可以更加直观的看出某篇文章的故事梗概. 首先贴出一张词云图(以哈利波特小说为例): 在生成词云图之前 ...

  6. 查看list的形状_用Wordcloud生成指定形状的词云图

    wordcloud是Python扩展库中一种将词语用图片表达出来的一种形式,通过词云生成的图片,我们可以更加直观的看出某篇文章的故事梗概. 首先贴出一张词云图(以哈利波特小说为例): 在生成词云图之前 ...

  7. 在Python中用WordCloud生成聊天记录热点词汇词云图

    刚开学第一天,因为老师没在实验室,所以就没去.之前有看到过别人展示过热点词汇的那种图片,闲着也是闲着,所以就捣鼓了一下午,为了整理一下学的东西,下面就以QQ聊天记录为例子,生成热点词汇.具体的效果如下 ...

  8. python词云图_Python生成一篇文章的词云图

    使用前需要确保安装以下第三方库: jieba, wordcloud, imageio, sklearn,csv imageio.csv和sklearn在安装anaconda时默认是安装的,另外两个库需 ...

  9. python爬虫音乐图片的感受_python爬虫+词云图,爬取网易云音乐评论

    又到了清明时节,用python爬取了网易云音乐<清明雨上>的评论,统计词频和绘制词云图,记录过程中遇到一些问题 爬取网易云音乐的评论 一开始是按照常规思路,分析网页ajax的传参情况.看到 ...

最新文章

  1. silverlight 无法发布 如何灵活配置IP
  2. 第四十四节,subprocess系统命令模块
  3. 一次检验自己技术和耐力的碎片提取经历
  4. 使用NPOI将数据库里信息导出Excel表格并提示用户下载
  5. Oracle数据类型与.NET中的对应关系
  6. ubuntu php7.4,在Ubuntu 18.04/19.04/16.04版本上安装PHP 7.4的简单方法
  7. Java写入的常用技巧
  8. Idea 连接数据库出错Connection to xxx@localhost failed. [08001] Could not create connection to database
  9. 汉庭季琦:我用IT精神打造传统产业
  10. 下面程序段中带下划线的语句的执行次数的数量级是( )
  11. Electron中的消息通知
  12. SAP 如何打Note
  13. 高性能mysql 随笔
  14. 数据库设计--数据流图(DFD)
  15. 几家线上少儿英语(vipkid、哒哒英语等)试听感想
  16. FPGA 中时钟信号的总结
  17. 深受Java程序员欢迎的10款Java IDE
  18. TeamSpeak3服务器搭建
  19. python 身份证号的有效性、生日(阴历阳历)、星座、生肖、性别、年龄、户籍所在地测试
  20. GRE数学入门级词汇

热门文章

  1. 语雀批量导出与图片下载
  2. !include: could not find: “nsProcessW.nsh“
  3. [技术] OIer的C++标准库 : STL入门
  4. Ardupilot源码框架
  5. 在Centos6/RHEL6上恢复ext4文件系统下误删除的文件
  6. 几何图形变化(Codevember)
  7. gRPC-拦截器简单使用
  8. python爬虫,python学习,如何用python爬取视频资源
  9. CentOS 7 minimal安装完成之后安装图形界面
  10. 解决Win10系统“win32错误报告,拒绝访问”问题,正解!!!