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

 채택된 답변

Guillaume
Guillaume 2014년 8월 15일
편집: Guillaume 2014년 8월 15일

2 개 추천

Personally, if I was going to do that encoding several times, I would precompute the encoding for all the possible values (there's only 256 of them) and use the precomputed array to do the conversion using basic matrix indexing. Thus the expensive calculation you only do once and the conversion from matrix to sequence is a simple array lookup, thus very fast.
You can precompute the array many different ways. A fairly simple one:
sequences = 'ACTG'; %in the order 0,1,2,3
seqbase4 = dec2base(0:255, 4); %convert integer 0-255 to a sequence of 0123
int2sequence = num2cell(sequences(seqbase4-'0'+1), 2); %represents integer 0-255 as cell array of ACTG
Whenever you want to convert a matrix, it's then as simple as:
a=imread('C:\Users\Abzz\Desktop\lena.png');
s=int2sequence(a+1); %+1 because matlab indexes start at 1

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 15일
편집: Azzi Abdelmalek 2014년 8월 15일

0 개 추천

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
Abirami 2014년 8월 15일
sir i understood the concept but i don't know how to apply it for the matrix..if i employ a loop im getting it wrong..pls help..
Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 15일
How is your matrix? give a short example
M={'10110001','10010010'} % Your matrix
str='10110001'
c='ACTG'
out=cellfun(@(x) c(bin2dec(reshape(x,2,[])')+1),M,'un',0)
Abirami
Abirami 2014년 8월 15일
편집: Abirami 2014년 8월 15일
a={'00111011' '01110011' '11011101' '10110111'} sir this is just a sample. imagine a 256*256 matrix.
Guillaume
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
Abirami 2014년 8월 15일
No sir initially the matrix is a decimal valued one which i convert to 8bit binary sequence..
Guillaume
Guillaume 2014년 8월 15일
Then see my answer. You have all the steps there.
Abirami
Abirami 2014년 8월 16일
still not getting it sir..i'm new to Matlab so im not able to understand it easily..can u pls elaborate sir so that i correct my code...
Guillaume
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
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에 대해 자세히 알아보기

질문:

2014년 8월 15일

편집:

2014년 8월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by