xor function
3 次查看(过去 30 天)
显示 更早的评论
A = ['010','110','111','011',...];
how calculate XOR as B = xor(010,110) >> B 110 then this result xor with the third sting '111'>>>> 001 then this result xor with fourth string '011' and so on
0 个评论
回答(2 个)
Walter Roberson
2012-5-5
To get you started:
char('0' + (A{1} ~= A{2})
6 个评论
Walter Roberson
2012-5-6
A = ['011' '001'] means the same as
A = horzcat('011', '001'); which is the same as
A = '011001';
Is that what you want, or do you wish to use cell arrays, which would have the advantage of being able to use the code I already gave?
Note: my previous posting was Saturday afternoon in my time zone, after which I had to do grocery shopping and similar activities, after which I had my first evening out since New Years Eve. If you require responses more quickly than volunteers can reasonably be expected to be available, then it is recommended that you hire a consulting firm.
Andrei Bobrov
2012-5-5
A = {'010','110','111','011'};
A1 = cellfun(@(x)x-'0',A,'un',0);
vertxor = rem(sum(cat(1,A1{:})>0),2);
or:
A =[...
0 1 0
1 1 0
1 1 1]
vertxor = rem(sum(A>0),2)
ADDED after Aseel's comment
use cell array (array A)
A = {'011' '001' '101'
'000' '110' '010'};
A1 = cell2mat(reshape(cellfun(@(x)x - '0',A,'un',0),size(A,1),1,[]));
B = cellstr(reshape(sprintf('%d%d%d',rem(sum(A1>0,3),2)),size(A,1),[]))
ADDED after Walter's comment:
A1 = cell2mat(reshape(cellfun(@(x)x - '0',A,'un',0),size(A,1),1,[]));
B = mat2cell(reshape(sprintf('%d',rem(cumsum(A1~=0,3),2)),2,[]),[1 1],[3 3 3]);
2 个评论
Walter Roberson
2012-5-6
Andrei, the question requires that the calculation be done step by step, for reasons not stated.
Walter Roberson
2012-5-6
Step by step does not allow the cellfun solution at all: the poster (for reasons unknown) requires that the xor of the first two sets of data be fully completed and expressed in character for before the second xor is allowed to be started. Efficiency is not the concern in the question: instead the concern is that it be done step-by-step.
Or at least that's the way *I* interpret the question.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!