MATLAB: Hi,,,lets say I have a 3*4 matrix.. I want to add row-wise elements. How to do that? TIA

addadditionmartix operationmatrixrow matrix

example : A=[1 0 1 0 ; 0 1 0 0 ; 1 1 1 0];
How can I add the elements row wise?

Best Answer

bsxfun(@xor,A,A)
does not have index to point operation along a given matrix dimension, bsxfun(@xor,A,A,2) doesn't work.
However, for vertical xor you can do:
[sz1 sz2]=size(A)
B=xor(A(1,:),A(2,:));for k=2:1:sz1-1 B=xor(B,A(k+1,:)); end
and horizontal xor:
[sz1 sz2]=size(A)
B=xor(A(:,1),A(:,2));for k=2:1:sz2-1 B=xor(B,A(:,k+1)); end
1 is vertical, 2 is horizontal
Perhaps you are going to repeat these operations, you may want to turn these 2 into a single function with input the dimension to calculate xor along.
In case you don't know how to, just ask as comment in this question and I'll have a look.
If you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John