how to perform bitwise XOR operation and scramble two matrices

조회 수: 10 (최근 30일)
Abirami
Abirami 2014년 8월 23일
댓글: Image Analyst 2018년 11월 18일
Hello
I need to perform XOR operation for four variables where each of them are represented as follows
A=00
G=01
C=10
T=11
and generate a table which gives the values for all combinations
XOR A G C T
A A G C T
G G A T C
C C T A G
T T C G A
This is the XOR rule im trying to apply on two matrices.... for eg:
A = 'GATT' 'AACT' 'ACAC' 'TTGA' 'GGCT'
'GCAC' 'TCAT' 'GTTC' 'GCCT' 'TTTA'
'AACG' 'GTTA' 'ACGT' 'CGTC' 'TGGA'
'CTAC' 'AAAA' 'GGGC' 'CCCT' 'TCGT'
'GTGT' 'GCGG' 'GTTT' 'TTGC' 'ATTA'
B= 'ATAC' 'AAAT' 'AGCT' 'AAGC' 'AAGT'
'TAGG' 'AAGT' 'ATGA' 'AAAG' 'AAGA'
'TAGC' 'CAGT' 'AGAT' 'GAAG' 'TCGA'
'GCTA' 'TTAC' 'GCCA' 'CCCC' 'TTTC'
'CCAA' 'AGGA' 'GCAG' 'CAGC' 'TAAA'
C= A XOR B
for eg consider the first element...
A XOR B= GATT XOR ATAC= GTTG
i want to do this for the whole matrix.Will it be easy if a function is defined and then called?? pls help..thanks in advance..

채택된 답변

Guillaume
Guillaume 2014년 8월 24일
편집: Guillaume 2014년 9월 3일
I would precompute two lookup tables (arrays) to perform the conversion from letters to numbers and back:
l2n('AGCT') = [0 1 2 3];
n2l= 'AGCT';
You only need to calculate l2n and n2l once and you can then convert from a letter sequence to numbers with:
s = 'GATT';
n = l2n(s); %gives n = [1 0 3 3].
And back:
sb = n2l(n+1); % +1 as matlab indices start at 1. gives sb='GATT'.
Calculating the xor of two sequences is then:
a = 'GATT';
b = 'ATAC';
c= n2l(bitxor(l2n(a), l2n(b)) + 1)
c =
'GTTG'
and for two cell arrays of the same size:
A = { 'GATT' 'AACT' 'ACAC' 'TTGA' 'GGCT'
'GCAC' 'TCAT' 'GTTC' 'GCCT' 'TTTA'
'AACG' 'GTTA' 'ACGT' 'CGTC' 'TGGA'
'CTAC' 'AAAA' 'GGGC' 'CCCT' 'TCGT'
'GTGT' 'GCGG' 'GTTT' 'TTGC' 'ATTA'};
B = { 'ATAC' 'AAAT' 'AGCT' 'AAGC' 'AAGT'
'TAGG' 'AAGT' 'ATGA' 'AAAG' 'AAGA'
'TAGC' 'CAGT' 'AGAT' 'GAAG' 'TCGA'
'GCTA' 'TTAC' 'GCCA' 'CCCC' 'TTTC'
'CCAA' 'AGGA' 'GCAG' 'CAGC' 'TAAA'};
C = cellfun(@(sa, sb) n2l(bitxor(l2n(sa), l2n(sb)) + 1), A, B, 'UniformOutput', false)
C =
'GTTG' 'AACA' 'ATCG' 'TTAC' 'GGTA'
'CCGT' 'TCGA' 'GACC' 'GCCC' 'TTCA'
'TATT' 'TTCT' 'ATGA' 'TGTT' 'ATAA'
'TGTC' 'TTAC' 'ATTC' 'AAAG' 'AGCG'
'TGGT' 'GTAG' 'AGTC' 'GTAA' 'TTTA'
edited for spelling

추가 답변 (3개)

Image Analyst
Image Analyst 2014년 8월 23일
Why'd you tag it with image processing? And can't you use repmat() to extend your row and column vectors into a full matrix then just do xor(a,b)?
  댓글 수: 1
Abirami
Abirami 2014년 8월 24일
im actually performing this for two encoded images....thats why,....

댓글을 달려면 로그인하십시오.


Geoff Hayes
Geoff Hayes 2014년 8월 23일
편집: Geoff Hayes 2014년 8월 23일
Abirami - try the following.
% create the char vector in this order, since A==0, G==1, C==2,
% and T==3, the index for any of these elements will be its binary
% value plus one i.e. G is 01 so its index is 1+1=2
char_array = ['A' ; 'G' ; 'C' ; 'T'];
% pre-allocate memory to the output matrix
char_mtx = cell(4,4);
% now iterate over each pair from the char array
for u=1:4
for v=1:4
% do the bitxor: note how we subtract one since u,v start
% at one
res = bitxor(u-1,v-1);
% copy the character, adding one, to the output matrix
char_mtx{u,v} = char_array(res+1);
end
end
The output is
char_mtx =
'A' 'G' 'C' 'T'
'G' 'A' 'T' 'C'
'C' 'T' 'A' 'G'
'T' 'C' 'G' 'A'
Try the above and see what happens!

aaru sri
aaru sri 2018년 11월 18일
Error using bitxor
Double inputs must have integer values in the range of ASSUMEDTYPE.
%%%%%above is the error coming
fx1=bitxor(fx,j1,)
fx contains the negative values also
how can i solve this or what mistake i m making
  댓글 수: 1
Image Analyst
Image Analyst 2018년 11월 18일
This is not an answer for Abirami's question. Please read this link and then post your own answer. Since you will have read that link, your new question will of course include more data (images, code, etc.) that will allow people to solve your mistake, unlike now.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by