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

Stephen23
Stephen23 2017년 10월 22일
@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.

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

 채택된 답변

Birdman
Birdman 2017년 10월 22일

0 개 추천

x={'1' '1' '1' '0' '1' '1' '0' '1'};
v={'1' '1' '0' '0' '1' '0' '1' '1'};
for i=1:1:length(x)
xx(i)=str2double(cell2mat(x(i)));
vv(i)=str2double(cell2mat(v(i)));
if(xx(i)==vv(i))
XOR(i)=0;
else
XOR(i)=1;
end
end
Hope this helps.

댓글 수: 4

Stephen23
Stephen23 2017년 10월 22일
편집: Stephen23 2017년 10월 22일
See my answer for a much simpler and more efficient solution
Birdman
Birdman 2017년 10월 22일
Checked it and saw that it is more efficient but it is an already defined function. If he tries to understand my way, he will learn and understand the algorithm of the xor gate.
Darsana P M
Darsana P M 2017년 10월 22일
thanks a lot sir
Jan
Jan 2017년 10월 22일
@Çevikalp Sütunç: In the line:
xx(i)=str2double(cell2mat(x(i)))
the cell2mat can be replaced by a simpler cell indexing with curly braces:
xx(i) = str2double(x{i})
But str2double works on cell strings directly also:
xx(i) = str2double(x(i))
or better outside the loop:
xx = str2double(x)

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

추가 답변 (4개)

Stephen23
Stephen23 2017년 10월 22일
편집: Stephen23 2017년 10월 22일

2 개 추천

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

Darsana P M
Darsana P M 2017년 10월 22일
Thanks a lot sir. I think this is much more simpler. I took them as cell array because i was dealing with hexa decimal values. I was trying out this with small values.
So this gives xor of x and v. 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 same for v1,v2 and so on. So while applying loops how can i access the entire set of elements and find the xor of each. ie xor(x1,v1), xor(x2,v2) etc

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

KL
KL 2017년 10월 22일

1 개 추천

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

Jan
Jan 2017년 10월 22일
Or directly:
res = (str2double(x) ~= str2double(v));
KL
KL 2017년 10월 22일
Oh yeah! Thanks Jan.
Darsana P M
Darsana P M 2017년 10월 23일
편집: Stephen23 2017년 10월 23일
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 same for v1,v2 and so on. So while applying loops how can i access the entire set of elements and find the xor of each. ie xor(x1,v1), xor(x2,v2) etc
Stephen23
Stephen23 2017년 10월 23일
"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.

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

Jan
Jan 2017년 10월 22일
편집: Jan 2017년 10월 22일

1 개 추천

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
Christina Christofidi 2020년 1월 22일

0 개 추천

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

Jan
Jan 2020년 2월 2일
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.

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

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

질문:

2017년 10월 22일

댓글:

Jan
2020년 2월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by