number of values greater than zero in a large matrix

7 views (last 30 days)
I have a 900X25 matrix The matrix has rows of zeros with one or two numeric values. Some rows have all zeros. What I want to do is have Matlab go through each row of zeros and find the lowest numeric value in that row, or if there is all zeros in that row, then the code will just skip that row. I want the code to return a 1X25 or a 25X1 array which will let me know the number of times there was a numeric value in each column. And not counting the larger numbers if there were more than one number in a given row. Example of data:
Grandmatrix = 23 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 87 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 93 12 0 0 0 0 0 0 0 0 0 0 0 0 0
Reducedmatrix = 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0
  4 Comments
Sean de Wolski
Sean de Wolski on 18 Jun 2012
Andrei and I are clearly missing the point. ZCould you give us a small example (3x3 maybe) and explain.
charles atlas
charles atlas on 18 Jun 2012
On the answer that I provided bleow I explained it.
The code: Reducedmatrix = sum(Grandmatrix~=0); did exactly what I wanted except it took all nonzero values and provided the number of times there was a numeric value in a column for each column and stored it in the array "Reducedmatrix". What I wanted was the number of every minimum nonzero number from every row unless the row had all zeros.
For example, the code would look at the example from Grandmatrix above and go though the rows and count onre for every time it came across a number not equal to zero in a row. When the code came to the row that had the "93" and the "12" in it, it would count 1 for the "12" because it is the lower of the two numeric values greater than zero in that row. It would not count "93" because 93 is greater than 12. so it would say that for that row there was another number in column 12 a nd not in column 11. "Reducedmatrix = sum(Grandmatrix~=0);" would have counted "1" for the 93 and the 12.

Sign in to comment.

Answers (3)

Andrei Bobrov
Andrei Bobrov on 18 Jun 2012
Reducedmatrix = any(Grandmatrix);
or?
Reducedmatrix = sum(Grandmatrix~=0);

charles atlas
charles atlas on 18 Jun 2012
Reducedmatrix=sum(Grandmatrix~=0); works the best. The problem is, it counts numeric data when there is more than one nonzero value in a row. I want the code to only count the lower number when it counts a row with two or more nonzero number. The answer I recieved when I ran the code was Reducedmatrix=[12 3 4 8 32 4 0 1 5 12 14 0 0 2 0 13 16 18 5 2 0 1 9 16 16]; when it should have been: Reducedmatrix=[12 2 4 8 29 3 0 1 5 11 13 0 0 2 0 12 16 17 5 1 0 1 8 15 15];
All help is very greatly appreciated,
-Atlas

Walter Roberson
Walter Roberson on 18 Jun 2012
sum(GrandMatrix == repmat( min(GrandMatrix,2), 1, size(GrandMatrix,2) ))
  2 Comments
charles atlas
charles atlas on 19 Jun 2012
I tried this code and an error came up that said "Error using eq matrix dimensions must match." I'm not sure why this is except for the fact that when I do M=repmat( min(GrandMatrix,2), 1, size(GrandMatrix,2));
I get a matrix M=900X625. I am not exactly sure why this is doing that but I know that somehow the number of columns is getting squared (25^2 = 625).
Walter Roberson
Walter Roberson on 19 Jun 2012
Dang, I keep forgetting about the other argument to min()
sum(GrandMatrix == repmat( min(GrandMatrix,[],2), 1, size(GrandMatrix,2) ))

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!