目录
1. 图片转为ASCII字符
2. 生成迷宫
3. 生成词云图
这个比较有意思,可以将一个图像转换为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;;;;;;;;;;;;;;::::,,,,,,.........................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
这是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
% [email protected]
%
% 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_spot
switch double(key(1))
case 108 % left
if ptr_left(current_id)<0 % check for legal move
current_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');
end
case 114 % right
if ptr_right(current_id)<0 % check for legal move
current_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');
end
case 117 % up
if ptr_up(current_id)<0 % check for legal move
current_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');
end
case 100 % down
if ptr_down(current_id)<0 % check for legal move
current_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');
end
otherwise
end
end
thyme=toc;
title(cat(2,' Winning Time ',num2str(round(thyme*100)/100),'(sec)'),'FontSize',20)
return
function move_spot(src,evnt)
assignin('caller','key',evnt.Key)
return
function 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 blocked
line([cc(ii)+.5,cc(ii)+.5],[rr(ii)-.5,rr(ii)+.5]);
hold on
end
if ptr_down(ii)>0 % down passage blocked
line([cc(ii)-.5,cc(ii)+.5],[rr(ii)+.5,rr(ii)+.5]);
hold on
end
end
axis equal
axis([.5,col+.5,.5,row+.5])
axis off
set(gca,'YDir','reverse')
return
function [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 region
tid=ceil(col*row*rand(15,1)); % get a set of temporary ID's
cityblock=cc(tid)+rr(tid); % get distance from the start
is_linked=(state(tid)==1); % The start state is in region 1 - see if they are linked to the start
temp = sortrows(cat(2,tid,cityblock,is_linked),[3,2]); % sort id's by start-link and distance
tid = 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 added
switch upper(pattern)
case 'C' % checkerboard
dir = ceil(8*rand);
nb=3;
block_size = min([row,col])/nb;
while block_size>12
nb=nb+2;
block_size = min([row,col])/nb;
end
odd_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>6
dir=4;
end
if dir>4
dir=3;
end
else
if dir>6
dir=2;
end
if dir>4
dir=1;
end
end
case 'B' % burst
dir = ceil(8*rand);
if abs((rr(tid)-row/2))6
dir=4;
end
if dir>4
dir=3;
end
else
if dir>6
dir=2;
end
if dir>4
dir=1;
end
end
case 'S' %spiral
dir = ceil(8*rand);
if abs((rr(tid)-row/2))>abs((cc(tid)-col/2))
if dir>6
dir=4;
end
if dir>4
dir=3;
end
else
if dir>6
dir=2;
end
if dir>4
dir=1;
end
end
case 'V'
dir = ceil(8*rand);
if dir>6
dir=4;
end
if dir>4
dir=3;
end
case 'H'
dir = ceil(8*rand);
if dir>6
dir=2;
end
if dir>4
dir=1;
end
otherwise % random
dir = 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 dir
case -1
case 1
if 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);
end
case 2
if 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);
end
case 3
if 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);
end
case 4
if 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);
end
otherwise
dir
error('quit')
end
end
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 1
close all
score(ii,1) = maze(rr,cc,'r')
pause(5)
rr=round(rr*1.12);
cc=round(cc*1.12);
ii=ii+1;
end
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 Color
case '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
end
freq=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); % Initialize
for 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~=jj
Q_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))));
end
end
POS_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 off
axis off
end
%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~=jj
Q_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))));
end
end
POS_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 off
axis off