# Inner character permutation with MATLAB
A while back I found myself in sort of “reading slump” where I tended to gloss over the words and recognize them really darn well without actually paying enough attention to actually make good sense of the meaning. Deciding that the problem was that reading was just too easy, I thought it might be beneficial to decrease my capacity immediate recognition in order to force me to remain mentally engaged at a basic structural level by scrambling the inner letters (i.e. letters not at the word boundary) of whatever given text. A demo can be found here.
The code for this conversion is pretty simple and you’ll notice that the bulk of it is just formatting and customizing to handle my favored data format. The function which executes the letter permutation in particular uses a string generated by randperm as the replacement string for regexprep – which I think is a pretty cool MATLAB feature.
function permstr=tpm2(inputfilename,outputfilename) str=fileread(inputfilename); %reads file m=findstr(str,char(10)); q=findstr(str,[char(10) char(13)]); q=[q q+2]; s=setdiff(m,q); s=[s s-1]; str(s)=char(32); %formats linebreaks in a way which pleases me permstr=regexprep(str, '(?<=\w)\w{2,}(?=\w)', '${$0(randperm(length($0)))}'); %textpermutation fid=fopen(outputfilename,'w'); fprintf(fid,permstr); fclose(fid); %writes file |