How the computation happening : sum(A~=0,2)

> A
A =
1 2 3
4 5 6
7 8 9
>> sum(A)
ans =
12 15 18
>> sum(A~=0,2)
ans =
3
3
3

 Accepted Answer

madhan ravi
madhan ravi on 16 Mar 2019
Edited: madhan ravi on 16 Mar 2019
>>A~=0 % creates a logical matrix so when you sum along rows, you get three
ans =
3×3 logical array
1 1 1
1 1 1
1 1 1
>> sum(ans,2)
ans =
3
3
3
>> sum(A,2) % perhaps you mean't to do this
ans =
6
15
24

More Answers (0)

Products

Release

R2018b

Tags

Community Treasure Hunt

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

Start Hunting!