Reputation: 2059
Here's a problem that is surprisingly hard for me to solve and I'm sure there must be an elegant solution: if any of the colums in the follow matrix a_mat
contains exactly one logical 1
, output 1
, otherwise output 0
.
a_mat=[0 1 1 0; ...
1 0 1 0; ...
0 1 0 0];
solution:
sol_mat=[1 0 0 0];
Is there an 'easy' way to solve this problem using binary operators including xor etc? I used setxor()
for previous, similar problems, however cannot get it to work with only one input-argument.
Upvotes: 0
Views: 37
Reputation: 18838
You can do it using sum
:
sol_mat = sum(a_mat, 1) == 1
It's working fast enough in matlab.
Upvotes: 4