How to do xor operation?
Mostrar comentarios más antiguos
How to do xor operation in cell array. Suppose i have bits x={'1' '1' '1' '0' '1' '1' '0' '1'}; and v={'1' '1' '0' '0' '1' '0' '1' '1'}; Should i use cell function? Can somebody help me with the correct code.
1 comentario
Stephen23
el 22 de Oct. de 2017
@Darsana P M: why are you storing this data in cell arrays? Your code would be a lot simpler and more efficient if you stored character data in a char array, or used logical/numeric arrays. Cell arrays do not seem to serve any useful purpose here.
Respuesta aceptada
Más respuestas (4)
Ugly loops are not required, this is all you need:
>> x = {'1' '1' '1' '0' '1' '1' '0' '1'};
>> v = {'1' '1' '0' '0' '1' '0' '1' '1'};
>> xor([x{:}]-'0',[v{:}]-'0')
ans =
0 0 1 0 0 1 1 0
If you had stored your data in a simpler character array, then all you would need is this:
>> x = '11101101'; v = '11001011';
>> xor(x-'0',v-'0')
ans =
0 0 1 0 0 1 1 0
Compare with Çevikalp Sütunç's answer: which one is going to be more efficient, is easier to understand the purpose of, and will be easier to debug?
1 comentario
Darsana P M
el 22 de Oct. de 2017
KL
el 22 de Oct. de 2017
Another method maybe,
x = {'1' '1' '1' '0' '1' '1' '0' '1'};
v = {'1' '1' '0' '0' '1' '0' '1' '1'};
res = logical(zeros(size(x)));
res(str2double(x)~=str2double(v)) = logical(1);
As Stephen says, having a cell array here is unnecessary.
4 comentarios
Jan
el 22 de Oct. de 2017
Or directly:
res = (str2double(x) ~= str2double(v));
KL
el 22 de Oct. de 2017
Oh yeah! Thanks Jan.
Darsana P M
el 23 de Oct. de 2017
Editada: Stephen23
el 23 de Oct. de 2017
Stephen23
el 23 de Oct. de 2017
"If suppose i have few vectors as x1={'1' 1' 0 1 1 0 1 0} x2={ 1 1 0 0 1 1 1 0} upto xn..."
Do not do this. Magically accessing variables names is complex, slow, buggy, and hard to debug. Read this to know why:
Much neater, faster, and more efficient would be if you simply stored all of your data in one array (which could be an ND numeric array, or a cell array). Using indexing is simple, fast, easy to debug, and very efficient.
It is strange, that the inputs are char's, but there is no need to convert them:
x = {'1' '1' '1' '0' '1' '1' '0' '1'};
v = {'1' '1' '0' '0' '1' '0' '1' '1'};
L = {'0', '1'};
Result = L(~strcmp(x, v) + 1)
Christina Christofidi
el 22 de En. de 2020
0 votos
Hey, I have an employment about the network coding. I need to run a file with some numbers and do XOR between them. You can help me????
1 comentario
Jan
el 2 de Feb. de 2020
Please do not attach a new question in the section for answers of another question. Create your won question instead. By the way, this seems to be a job for a simple xor comand.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!