# how to perform bitwise XOR operation and scramble two matrices

1 Ansicht (letzte 30 Tage)
Abirami am 23 Aug. 2014
Kommentiert: Image Analyst am 18 Nov. 2018
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..
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Guillaume am 24 Aug. 2014
Bearbeitet: Guillaume am 3 Sep. 2014
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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (3)

Image Analyst am 23 Aug. 2014
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Abirami am 24 Aug. 2014
im actually performing this for two encoded images....thats why,....

Melden Sie sich an, um zu kommentieren.

Geoff Hayes am 23 Aug. 2014
Bearbeitet: Geoff Hayes am 23 Aug. 2014
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!
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

aaru sri am 18 Nov. 2018
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Image Analyst am 18 Nov. 2018

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by