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

1 次查看(过去 30 天)
example : A=[1 0 1 0 ; 0 1 0 0 ; 1 1 1 0];
How can I add the elements row wise?

采纳的回答

John BG
John BG 2016-5-13
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
  4 个评论
John BG
John BG 2016-5-14
a start point to develop the function you want could be the following:
function A_xor=auto_xor(A,n)
% n can be 1: xor horizontally, all columns
% or 2: xor vertically, all rows
[sz1 sz2]=size(A);
switch n
case 2
A_xor=xor(A(1,:),A(2,:));
for k=2:1:sz1-1
A_xor=xor(A_xor,A(k+1,:));
end
case 1
A_xor=xor(A(:,1),A(:,2));
for k=2:1:sz2-1
A_xor=xor(A_xor,A(:,k+1));
end
otherwise
% display something with 'error' and break
end
end
it needs some work, to be a proper function that stands erroneous inputs, but hope it helps. Thanks for the points Regards
John

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by