Name='Morgan';
N=perms(Name); % Careful with this, as there are N! permutations.
[r,c]=size(N); % r is the number of rows, equal to N!
M=repmat(Name,r,1); % creates a matrix with r copies of ‘Morgan'
matchNM=(N==M); % compare each letter in N and M and if they are the same,
% write a 1 in the same location in matchNM;
% Note the double equal sign == for ‘logical equivalence'
summatches=sum(matchNM'); % Sum the row totals, note it is sum matchNM transpose
i=find(summatches==c); % Find the row or rows where every letter matches, the sum is 6;
% The previous statements can be condensed into one line if you want.
i=find(sum((N==repmat(Name,r,1))')==c);
format rat;
p=length(i)/r % What is the probability that a permutation of 'Name' produces the
% correct spelling?
format