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?
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!