Borrar filtros
Borrar filtros

Input or put a large array of numbers into a function

2 visualizaciones (últimos 30 días)
Ben Nguyen
Ben Nguyen el 28 de Oct. de 2022
Comentada: Ben Nguyen el 2 de Nov. de 2022
I'm fairly new to MATLAB. My assignment is to pick a word, break it down into each letter, generate all the possibilities with replacement of all the letters, assign each letter with a probability, calculate the their probabilities in 3rd, 4th, and 5th order extenstion, and use the built-in huffmandict function to generate the binary equivalence of each symbol.
In my case, I picked the word 'locate', so each letter will be 'l' 'o' 'c' 'a' 't' 'e'.
3rd order extension means combination of 3 letters. For instance, 'loc' 'loa' 'lot' etc... The same goes for 4th and 5th order. So 3rd order will have 216 possible combinations, 4th order will have 1296 and 5th will have 7776 combinations.
From my thought process to approach this problem, here is what I have so far:
I used the COMBINATOR function that I found to generate all the possible combinations
n3 = combinator(6, 3, 'p', 'r'); % 3rd order possible combinations
n4 = combinator(6, 4, 'p', 'r'); % 4th order possible combinations
n5 = combinator(6, 5, 'p', 'r'); % 5th order possible combinations
then I move the outputs of n3, n4, and n5 to Excel, replace the number with the letters (1 = l , 2 = o , 3 = c , 4 = a, 5 = t , 6 = e) , then replace them again with assigned probabilities (l = 0.1 , o = 0.18 , c =0.02 , a = 0.4 , t = 0.08 , e = 0.22), calcalate the probabilities for each symbol by the product of all the letter in that symbol. For instance, 3rd order, 'loc', will be 0.1*0.18*0.02 = 0.00036. I'm currently stuck at how to move back all 216, 1296 or 7776 probabilties to MATLAB to use the huffmandict function. The function requires 2 arguments, numbers of symbols and their corresponding probabilities.
I hope this makes sense. Any advice, help, tip or guidance will be appreciated. I know all of these can be done on MATLAB, but I'm not fluent in the programming language. Main reason that I involved Excel.
  3 comentarios
Ben Nguyen
Ben Nguyen el 28 de Oct. de 2022
n3 = combinator(6, 3, 'p', 'r');
n4 = combinator(6, 4, 'p', 'r');
n5 = combinator(6, 5, 'p', 'r');
Ben Nguyen
Ben Nguyen el 28 de Oct. de 2022
That's all I had for my code. My next step is to transfer all the outputs of n3, n4, n5 to Excel spreasheets manually. Since the combinator function output the combinations only in numbers, I will replace the numbers with letters in Excel manually.

Iniciar sesión para comentar.

Respuesta aceptada

David Hill
David Hill el 28 de Oct. de 2022
x='locate';prob=[.1 .18 .02 .4 .08 .22];
p=[];order=3;
n=nchoosek(1:length(x),order);
for k=1:size(n,1)
p=[p;perms(n(k,:))];
end
X=x(p)
X = 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'
P=prod(prob(p),2)
P = 120×1
0.0004 0.0004 0.0004 0.0004 0.0004 0.0004 0.0072 0.0072 0.0072 0.0072
  6 comentarios
David Hill
David Hill el 29 de Oct. de 2022
Even better
x='locate';prob=[.1 .18 .02 .4 .08 .22];
order=3;
b=cell(1,order);
[b{:}] = ndgrid(1:length(x));
idx=[];
for k=1:length(b)
idx=[idx,b{k}(:)];
end
X=x(idx)
X = 216×3 char array
'lll' 'oll' 'cll' 'all' 'tll' 'ell' 'lol' 'ool' 'col' 'aol' 'tol' 'eol' 'lcl' 'ocl' 'ccl' 'acl' 'tcl' 'ecl' 'lal' 'oal' 'cal' 'aal' 'tal' 'eal' 'ltl' 'otl' 'ctl' 'atl' 'ttl' 'etl' 'lel' 'oel' 'cel' 'ael' 'tel' 'eel' 'llo' 'olo' 'clo' 'alo' 'tlo' 'elo' 'loo' 'ooo' 'coo' 'aoo' 'too' 'eoo' 'lco' 'oco' 'cco' 'aco' 'tco' 'eco' 'lao' 'oao' 'cao' 'aao' 'tao' 'eao' 'lto' 'oto' 'cto' 'ato' 'tto' 'eto' 'leo' 'oeo' 'ceo' 'aeo' 'teo' 'eeo' 'llc' 'olc' 'clc' 'alc' 'tlc' 'elc' 'loc' 'ooc' 'coc' 'aoc' 'toc' 'eoc' 'lcc' 'occ' 'ccc' 'acc' 'tcc' 'ecc' 'lac' 'oac' 'cac' 'aac' 'tac' 'eac' 'ltc' 'otc' 'ctc' 'atc' 'ttc' 'etc' 'lec' 'oec' 'cec' 'aec' 'tec' 'eec' 'lla' 'ola' 'cla' 'ala' 'tla' 'ela' 'loa' 'ooa' 'coa' 'aoa' 'toa' 'eoa' 'lca' 'oca' 'cca' 'aca' 'tca' 'eca' 'laa' 'oaa' 'caa' 'aaa' 'taa' 'eaa' 'lta' 'ota' 'cta' 'ata' 'tta' 'eta' 'lea' 'oea' 'cea' 'aea' 'tea' 'eea' 'llt' 'olt' 'clt' 'alt' 'tlt' 'elt' 'lot' 'oot' 'cot' 'aot' 'tot' 'eot' 'lct' 'oct' 'cct' 'act' 'tct' 'ect' 'lat' 'oat' 'cat' 'aat' 'tat' 'eat' 'ltt' 'ott' 'ctt' 'att' 'ttt' 'ett' 'let' 'oet' 'cet' 'aet' 'tet' 'eet' 'lle' 'ole' 'cle' 'ale' 'tle' 'ele' 'loe' 'ooe' 'coe' 'aoe' 'toe' 'eoe' 'lce' 'oce' 'cce' 'ace' 'tce' 'ece' 'lae' 'oae' 'cae' 'aae' 'tae' 'eae' 'lte' 'ote' 'cte' 'ate' 'tte' 'ete' 'lee' 'oee' 'cee' 'aee' 'tee' 'eee'
p=prod(prob(idx),2)
p = 216×1
0.0010 0.0018 0.0002 0.0040 0.0008 0.0022 0.0018 0.0032 0.0004 0.0072
Ben Nguyen
Ben Nguyen el 2 de Nov. de 2022
David, you are a gods-sent. Based on your code (the first one without the for loop since I understood it better), I improvised my own. It worked. Thank you so much!!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by