# How can I count how many numbers are different from '-1'?

2 views (last 30 days)
Davide Conti on 1 Nov 2019
Commented: Walter Roberson on 5 Nov 2019
Hi,
Example: Suppose we a have a matrix A
A= [1 -1 0 -1 0 0 -1 0 -1 -1 -1 -1 -1 -1 -1]
How can I count how many numbers are different from '-1'?
##### 2 CommentsShowHide 1 older comment
Davide Conti on 5 Nov 2019
Ok thanks, but if i wanted to do it for every line of my matrix?
For examples:
1 -1 0 -1 0 0 -1 0 -1
4 2 3 -1 0 3 1 2 -1
in this case for the first row the numbers different form '-1' are 5, in the second row they are 7.
With your loop it comes out that in the second row they are 12, because it considers the first row.

Fangjun Jiang on 1 Nov 2019
Walter Roberson on 5 Nov 2019
And then sum() along the appropriate axis.

Pedro Martinez on 1 Nov 2019
Total = sum (A==-1);
Walter Roberson on 5 Nov 2019
That gives the number of rows of -1 but the user wants the number of columns that are not -1

### Categories

Find more on Matrices and 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!

Translated by