how can make the xor return integer values instead of 1's or 0's?

1 回表示 (過去 30 日間)
JacobM
JacobM 2016 年 9 月 20 日
コメント済み: Star Strider 2016 年 9 月 20 日
In the following code, I am xor-ing two different sizes matrices, to get rid of the element overlap in any row of x and y (overlap happens when I have an integer element in x and an integer element in y in same position) example: x=[1 0]; y=[5 0]; 1 & 5 are consider overlapping. so it get rid of these two rows I am xor-ing the matrices.
The problem is that, when you xor you do a logical operation, whcih means the output is 1 or 0 but I want to reserve the element values, Ex: x=[1 0]; y=[0 5]; z=xor(x,y); z=[1 1]; while I want it to be z=[1 5];
Any hint? Is there any function can do xor-ing and reserving the value and not returning only 1 or 0? which means if c & v are any integers: xor(0,0)=xor(c,v)=0 xor(c,0)=c, xor(0,v)=v;
here is the code,
x=[0 1;1 0];
y=[2 0;0 2;2 2];
r=size(x,1);
z = cell(r,1); % intialize z
for n=1:r
% identify rows where there is no 1's overlap
k = ~any(bsxfun(@and,x(n,:),y),2);
% xor 'n' x row with the 'k' rows of y
z{n} = bsxfun(@xor,x(n,:),y(k,:));
end
S = vertcat(z{:});

採用された回答

Star Strider
Star Strider 2016 年 9 月 20 日
See if bitxor does what you want:
z{n} = bsxfun(@bitxor,x(n,:),y(k,:));
  8 件のコメント
JacobM
JacobM 2016 年 9 月 20 日
編集済み: JacobM 2016 年 9 月 20 日
sure and thanks for taking the time to reply to me
Star Strider
Star Strider 2016 年 9 月 20 日
My pleasure.
Andrei Bobrov can probably come up with a one or two line perfect solution, but it is totally escaping me this afternoon.
I will do what I can to help otherwise.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by