how to convert numstrng to integers and take lcm

consider out 2 =
1 0 0 1 0 1
1 1 1 1 0 1
1 1 0 1 1 1
if i need to take the lcm of the binary rows that have in out2 i am doing this:
converting each row to numstrng using: out3 = num2str(out2(:))
now i need to pick 1 to m rows of this string (m = 5 here) and convert it to decimal but it wont work: out 4 = bin2dec(out3(1:1:5))
then i need to take the lcm of out4
x = lcm(out4)
% but the lcm function wont work for more than two numbers which is pretty challenging here, how to work through this

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 24 de Abr. de 2013
Try this:
out2 = [
1 0 0 1 0 1
1 1 1 1 0 1
1 1 0 1 1 1];
out4 = out2*2.^(size(out2,2)-1:-1:0)';
out_pair = nchoosek(out4,2);
out5 = num2cell(out_pair,1);
out_last = [out_pair, lcm(out5{:})];

5 comentarios

Malik
Malik el 24 de Abr. de 2013
Thanks alot. This is giving me the out_last i needed. I couldn't loop the array correctly.
Malik
Malik el 24 de Abr. de 2013
can u explain this a bit, especially the first two lines...and why did u use nchoosek
Malik
Malik el 26 de Abr. de 2013
how can i reconvert this out_last to binary form again?
Malik
Malik el 26 de Abr. de 2013
such that out_last comes in binary form in a row matrix?
out = dec2bin(out_last(:,end)) - '0';

Iniciar sesión para comentar.

Más respuestas (1)

Vineet
Vineet el 24 de Abr. de 2013

0 votos

You can put all the numbers in an array and take LCM of 2 numbers at a time.
Say, you begin with the LCM of the first and the second number. Whatever LCM you get now, treat it as a new number and calculate LCM of this number and the next number in the list.
Run a loop for the same.

Categorías

Más información sobre Discrete Math en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Abr. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by