Generate all permutation or possibilities
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ben Nguyen
el 26 de Oct. de 2022
Editada: Bruno Luong
el 27 de Oct. de 2022
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.
0 comentarios
Respuesta aceptada
David Hill
el 26 de Oct. de 2022
Editada: David Hill
el 26 de Oct. de 2022
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)
2 comentarios
Bruno Luong
el 27 de Oct. de 2022
Editada: Bruno Luong
el 27 de Oct. de 2022
Can't find where youu describe the second part in the original question, but here we go
s = 'locate'
p = [0.2 0.2 0.2 0.1 0.1 0.2]; % corresponding probability for each letter of 'locate'
s='locate'
n=3;
c=nchoosek(s,n);
c=reshape(c(:,perms(n:-1:1)')',n,[])'
[~, i] = ismember(c, s);
pc = prod(p(i),2) % this is the probability of the char-row in c
Más respuestas (1)
Bruno Luong
el 26 de Oct. de 2022
s='locate'
n=3;
c=nchoosek(s,n);
c=reshape(c(:,perms(n:-1:1)')',n,[])'
Ver también
Categorías
Más información sobre Denoising and Compression en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!