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 Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by