sum the elements in each row of a two-column matrix with the condition that one of the two values in each row is zero

6 views (last 30 days)
Hi, I have a matrix with 2 columns:
A = [ 3710 0
0 3714
3735 0
3719 0
3708 0
0 3698
3716 0
0 3699
3726 0
3704 22
3701 0
3707 0
0 3727
0 3728
3717 0];
I would like to sum the elements in each row, i.e. sum(A,2), if and only if all the rows have an element with value "zero".
Otherwise find the rows where both elements are different from zero.
Any extremely simple solution like this?
sum(A,2,'condition that each row has an element with value zero')

Accepted Answer

David Hill
David Hill on 3 Feb 2022
s=sum(A,2);
idx=A(:,1)==0|A(:,2)==0;
s=s(idx);%sum of all rows where one of the columns==0
notZero=find(~idx);%row index of A where neither column==0

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!