First question Is this this the best way to calculate the permutation of a number (I don't want an array of digits)?
>> n = 123;>> x = str2double(cellstr(perms(int2str(n))))x = 321 312 231 213 132 123
I rather doubt it…
Second question If it is, what is the best way to calculate the permutation matrix of a number vector (assuming the number of digits is prescribed)?
For example (in this case the numbers from 111 to 999):
Permut = nan(6,889);for number = 111:999 counter = number-111+1; Permut(:,counter) = str2double(cellstr(perms(int2str(number)))); end
Or more generic:
numDigits = 3; nStart = str2double(strrep(num2str(ones( 1,numDigits)), ' ', ''));nFinal = str2double(strrep(num2str(repmat(9,1,numDigits)), ' ', ''));Permut = nan(factorial(numDigits),nFinal-nStart+1);for number = nStart:nFinal counter = number-nStart+1; Permut(:,counter) = str2double(cellstr(perms(int2str(number)))); end
Best Answer