How to XOR two cells from the same cell array?

1 visualización (últimos 30 días)
Abirami
Abirami el 18 de Mzo. de 2015
Comentada: Michael Haderlein el 18 de Mzo. de 2015
Hello,
I have one array of size 1x1024. I want to XOR each cell of the array. The first cell remains the same.It is as follows
Let T represent the position of each cell, then
CellT` = {Cell T XOR Cell(T-1) for T=2..1024
Cell T for T=1
Lets consider a 1x10 array.It is of the following form.
X= ['0000000000000001' '0000000000000010' '0000000000000011' '0000000000000100' '0000000000000101' '0000000000000110' '0000000000000111' '0000000000001000' '0000000000001001' '0000000000001010']
So I need to XOR the adjacent elements except the first one. Please help thanks in advance.
  1 comentario
Guillaume
Guillaume el 18 de Mzo. de 2015
Editada: Guillaume el 18 de Mzo. de 2015
It sounds that your array with cells is just a plain matlab matrix, in which case you'd be better off calling the cells elements
Cell arrays, whose elements are cells are something very different in matlab.

Iniciar sesión para comentar.

Respuesta aceptada

Michael Haderlein
Michael Haderlein el 18 de Mzo. de 2015
With "cell", do you mean you have a cell array or do you refer to the elements of a normal logical array? I suppose second, then it's just
T =
1 1 0 0 1
>> XORT=xor(T(2:end),T(1:end-1))
XORT =
0 1 0 1
In case it's really a cell array, you can either translate the cell array into a normal array (cell2mat) or apply cellfun here:
>> Tc=num2cell(T)
Tc =
[1] [1] [0] [0] [1]
>> XORTc=cellfun(@(x,y) xor(x,y),Tc(2:end),Tc(1:end-1))
XORTc =
0 1 0 1
Hope that's answering your question!
  2 comentarios
Abirami
Abirami el 18 de Mzo. de 2015
Thank you so much sir. I've edited the qn once again to specify what i have as the input.
Michael Haderlein
Michael Haderlein el 18 de Mzo. de 2015
With respect to your modified question, you can do the following:
>> T= {'0000000000000001' '0000000000000010' '0000000000000011' '0000000000000100' '0000000000000101' '0000000000000110' '0000000000000111' '0000000000001000' '0000000000001001' '0000000000001010'};
>> Tc=cellfun(@(x) x=='1',T,'uniform',false);
>> XORTc=[Tc(1) cellfun(@(x,y) xor(x,y),Tc(2:end),Tc(1:end-1),'uniform',false)];
The first use of cellfun translates the string arrays into logical arrays and the second performs the xor along with the concatenation with the first value of Tc.

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 18 de Mzo. de 2015
If you are operating on strings of '0' and '1', then the xor operation is the same as the ~= operation plus a conversion back to string.
binstrxor = @(binstr1, binstr2) char('0' + (binstr1 ~= binstr2));
Thus:
X = {'0000000000000001' '0000000000000010' '0000000000000011' '0000000000000100' '0000000000000101' '0000000000000110' '0000000000000111' '0000000000001000' '0000000000001001' '0000000000001010'};
Xxor = cellfun(binstrxor, X(1:end-1), X(2:end), 'UniformOutput', false)
Note that the strings must be the same length (same number of bits) for ~= to work.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by