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.

 채택된 답변

David Hill
David Hill 2022년 10월 26일
편집: David Hill 2022년 10월 26일
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

Ben Nguyen
Ben Nguyen 2022년 10월 26일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2022년 10월 26일
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'

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품

릴리스

R2022b

질문:

2022년 10월 26일

편집:

2022년 10월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by