Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
example : A=[1 0 1 0 ; 0 1 0 0 ; 1 1 1 0];
How can I add the elements row wise?

Akzeptierte Antwort

John BG
John BG am 13 Mai 2016
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 Kommentare
John BG
John BG am 14 Mai 2016
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
John BG
John BG am 14 Mai 2016
Bearbeitet: John BG am 14 Mai 2016
to test the function, you may use
A=randi([0 1],randi([2 10],1,1),randi([2 10],1,1))
John

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by