Generate all permutation or possibilities

I'm still fairly new to MATLAB, but got thrown at with a homework on Huffman coding. It requires to take a word, break it down to each letter, generate the all the possibilities in 3rd, 4th, and 5th order. For instance, in my case, I picked the word 'locate', then strip it down to 'l' 'o' 'c' 'a' 't' 'e'. 3rd order means I'll generate all the possilities/combinations for 3 positions out of 6 letters like 'loc' 'loa' 'lot' etc.. Same concept goes as 4th and 5th order. I had tried nchoosek function, it only give me the combinations (order matter), not all permution or possibilities. I hope it makes sense. Any guidance will be appreciated.

 Accepted Answer

x='locate';
p=[];order=3;
n=nchoosek(1:length(x),order);
for k=1:size(n,1)
p=[p;perms(n(k,:))];
end
x(p)
ans = 120×3 char array
'col' 'clo' 'ocl' 'olc' 'lco' 'loc' 'aol' 'alo' 'oal' 'ola' 'lao' 'loa' 'tol' 'tlo' 'otl' 'olt' 'lto' 'lot' 'eol' 'elo' 'oel' 'ole' 'leo' 'loe' 'acl' 'alc' 'cal' 'cla' 'lac' 'lca' 'tcl' 'tlc' 'ctl' 'clt' 'ltc' 'lct' 'ecl' 'elc' 'cel' 'cle' 'lec' 'lce' 'tal' 'tla' 'atl' 'alt' 'lta' 'lat' 'eal' 'ela' 'ael' 'ale' 'lea' 'lae' 'etl' 'elt' 'tel' 'tle' 'let' 'lte' 'aco' 'aoc' 'cao' 'coa' 'oac' 'oca' 'tco' 'toc' 'cto' 'cot' 'otc' 'oct' 'eco' 'eoc' 'ceo' 'coe' 'oec' 'oce' 'tao' 'toa' 'ato' 'aot' 'ota' 'oat' 'eao' 'eoa' 'aeo' 'aoe' 'oea' 'oae' 'eto' 'eot' 'teo' 'toe' 'oet' 'ote' 'tac' 'tca' 'atc' 'act' 'cta' 'cat' 'eac' 'eca' 'aec' 'ace' 'cea' 'cae' 'etc' 'ect' 'tec' 'tce' 'cet' 'cte' 'eta' 'eat' 'tea' 'tae' 'aet' 'ate'

2 Comments

Thank you David, your code produces what I was intended.
Any chance you can give me guidance on the second part?
I need to find the product of each posibility. For instance, I'll set l = 0.2 , o = 0.2, c = 0.2, so the product of 'loc' will be 0.2*0.2*0.2 = 0.008.
Any help will be appreciated!
Can't find where youu describe the second part in the original question, but here we go
s = 'locate'
s = 'locate'
p = [0.2 0.2 0.2 0.1 0.1 0.2]; % corresponding probability for each letter of 'locate'
s='locate'
s = 'locate'
n=3;
c=nchoosek(s,n);
c=reshape(c(:,perms(n:-1:1)')',n,[])'
c = 120×3 char array
'loc' 'lco' 'olc' 'ocl' 'clo' 'col' 'loa' 'lao' 'ola' 'oal' 'alo' 'aol' 'lot' 'lto' 'olt' 'otl' 'tlo' 'tol' 'loe' 'leo' 'ole' 'oel' 'elo' 'eol' 'lca' 'lac' 'cla' 'cal' 'alc' 'acl' 'lct' 'ltc' 'clt' 'ctl' 'tlc' 'tcl' 'lce' 'lec' 'cle' 'cel' 'elc' 'ecl' 'lat' 'lta' 'alt' 'atl' 'tla' 'tal' 'lae' 'lea' 'ale' 'ael' 'ela' 'eal' 'lte' 'let' 'tle' 'tel' 'elt' 'etl' 'oca' 'oac' 'coa' 'cao' 'aoc' 'aco' 'oct' 'otc' 'cot' 'cto' 'toc' 'tco' 'oce' 'oec' 'coe' 'ceo' 'eoc' 'eco' 'oat' 'ota' 'aot' 'ato' 'toa' 'tao' 'oae' 'oea' 'aoe' 'aeo' 'eoa' 'eao' 'ote' 'oet' 'toe' 'teo' 'eot' 'eto' 'cat' 'cta' 'act' 'atc' 'tca' 'tac' 'cae' 'cea' 'ace' 'aec' 'eca' 'eac' 'cte' 'cet' 'tce' 'tec' 'ect' 'etc' 'ate' 'aet' 'tae' 'tea' 'eat' 'eta'
[~, i] = ismember(c, s);
pc = prod(p(i),2) % this is the probability of the char-row in c
pc = 120×1
0.0080 0.0080 0.0080 0.0080 0.0080 0.0080 0.0040 0.0040 0.0040 0.0040

Sign in to comment.

More Answers (1)

s='locate'
s = 'locate'
n=3;
c=nchoosek(s,n);
c=reshape(c(:,perms(n:-1:1)')',n,[])'
c = 120×3 char array
'loc' 'lco' 'olc' 'ocl' 'clo' 'col' 'loa' 'lao' 'ola' 'oal' 'alo' 'aol' 'lot' 'lto' 'olt' 'otl' 'tlo' 'tol' 'loe' 'leo' 'ole' 'oel' 'elo' 'eol' 'lca' 'lac' 'cla' 'cal' 'alc' 'acl' 'lct' 'ltc' 'clt' 'ctl' 'tlc' 'tcl' 'lce' 'lec' 'cle' 'cel' 'elc' 'ecl' 'lat' 'lta' 'alt' 'atl' 'tla' 'tal' 'lae' 'lea' 'ale' 'ael' 'ela' 'eal' 'lte' 'let' 'tle' 'tel' 'elt' 'etl' 'oca' 'oac' 'coa' 'cao' 'aoc' 'aco' 'oct' 'otc' 'cot' 'cto' 'toc' 'tco' 'oce' 'oec' 'coe' 'ceo' 'eoc' 'eco' 'oat' 'ota' 'aot' 'ato' 'toa' 'tao' 'oae' 'oea' 'aoe' 'aeo' 'eoa' 'eao' 'ote' 'oet' 'toe' 'teo' 'eot' 'eto' 'cat' 'cta' 'act' 'atc' 'tca' 'tac' 'cae' 'cea' 'ace' 'aec' 'eca' 'eac' 'cte' 'cet' 'tce' 'tec' 'ect' 'etc' 'ate' 'aet' 'tae' 'tea' 'eat' 'eta'

Categories

Products

Release

R2022b

Asked:

on 26 Oct 2022

Edited:

on 27 Oct 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!