convert binary to charaacter
显示 更早的评论
hello,
i have a binary sequnce 10110001. and i want to encode based on the following rule
A=00
C=01
T=10
G=11
and the answer i get will be something like TGAC.
i want to do it for a 256*256 matrix each element having an 8bit binary sequence..i have generated the matrix by using the following code
>>a=imread('C:\Users\Abzz\Desktop\lena.png');
>>disp(a);
>>imshow(a);
>>for i=1:1:256
>>for j=1:1:256
>>b{i,j,1} = dec2bin(a(i,j),8);
>>end
>>end
>>disp(b)
pls help..thanks in advance
采纳的回答
更多回答(1 个)
Azzi Abdelmalek
2014-8-15
编辑:Azzi Abdelmalek
2014-8-15
str='10110001'
c='ACTG'
b={'00','01','10','11'}
ss=regexp(str,'.{2}','match')
[ii,jj]=ismember(ss,b)
out=c(jj)
%or
str='10110001'
c='ACTG'
out=c(bin2dec(regexp(str,'.{2}','match'))+1)
%or
str='10110001'
c='ACTG'
out=c(bin2dec(reshape(str,2,[])')+1)
10 个评论
Abirami
2014-8-15
Azzi Abdelmalek
2014-8-15
How is your matrix? give a short example
Azzi Abdelmalek
2014-8-15
M={'10110001','10010010'} % Your matrix
str='10110001'
c='ACTG'
out=cellfun(@(x) c(bin2dec(reshape(x,2,[])')+1),M,'un',0)
Guillaume
2014-8-15
Did you matrix starts out as binary strings or as integer? If the latter, you don't need to convert to binary (which is the wrong base anyway, you're operating in base 4 not base 2)
Abirami
2014-8-15
Guillaume
2014-8-15
Then see my answer. You have all the steps there.
Abirami
2014-8-16
Guillaume
2014-8-16
Have you tried what I wrote in my answer? Does it not work for you? If it doesn't, which step is the problem?
Azzi Abdelmalek
2014-8-16
编辑:Azzi Abdelmalek
2014-8-16
a=imread('C:\Users\Abzz\Desktop\lena.png');
[n,m]=size(a);
b=arrayfun(@(x) dec2bin(x,8),a,'un',0);
c='ACTG';
out=cellfun(@(x) c(bin2dec(reshape(x,2,[])')+1),b,'un',0)
Note that Guillaume's method is faster, and do not need to convert your matrix from decimal to binary
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!