## decimal to binary and grouping into another matrix

on 30 Oct 2012

### Andrei Bobrov (view profile)

i have to convert a decimal value to binary and group every two digits to

form a new one dimensional matrix whose size is 1,4*rows*cols

` i tried this its not working ie `

secret = [22 33

`         22 44]`

[rows,cols]=size(secret);

for i= 1 : rows

`    for j= 1: cols`
`        binarysecret{i,j}=dec2bin(secret(i,j),8);`
`    end`

end cc=1;

[r,c]=size(binarysecret)

for q = 1 : r

`    for q1 = 1 : c`
`         while(x<8)`
`               y=x+1;`
`            secretDNA{cc} =  binarysecret{q,q1}(x:y)  `
`         x=x+2;`
`        cc=cc+1;`
`       end`
`    end`

end

## Products

No products are associated with this question.

### Andrei Bobrov (view profile)

on 30 Oct 2012
Edited by Andrei Bobrov

### Andrei Bobrov (view profile)

on 30 Oct 2012

```bi = dec2bin(secret,8) - '0';
out = reshape(mat2cell(bi,ones(size(bi,1),1),2*ones(size(bi,2)/2,1))',1,[]);
```

or

```n = 8;
s = size(secret);
bi = rem(floor(reshape(secret',[],1)*pow2(1-n:0)),2);
out = reshape(mat2cell(bi,ones(prod(s),1),2*ones(n/2,1))',1,[]);
```

Sharen H

on 30 Oct 2012

thanks a lot

Sharen H

### Sharen H (view profile)

on 30 Oct 2012
` secret = [22 33`

33 44]

bi = dec2bin(secret,8) - '0';

out = reshape(mat2cell(bi,ones(size(bi,1),1),2*ones(size(bi,2)/2,1))',1,[]);

How to regroup the answer to get back the original secret

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi