Filter out nonzero number in array of n*2 dimension.

I have two column and 208k rows array, each row is event number. My array is like that,
3145728 0
0 3
0 3
0 3
3145728 0
0 192
0 0
384 0
48 0
3145728 0
3145728 0
0 384
0 3
2097152 0
0 3
3145728 0
0 3
0 3
0 3
16777216 0
This is only for 20 events.Here, it look like randomly numbered of zero and non zero(i.e one row non zero and other row zero and vice-versa for each event). So can anybody please give me idea that how to filter out if there are non zero numbers in the same row of both column through matlab script.

More Answers (2)

If I understand your question, you want to delete the rows with non-zero entries in both columns, e.g.,
idx = sum(A~=0,2)==2;
A(idx,:) = [];

6 Comments

More or less, however I want to put only row and column which has only non zero number in same row of both column. like if 50th row of column first has non zero number and 50th row of other column has also non zero number. I'm looking whether my array has non zero number in same row? or not.
What is the goal of this? Are you trying to find the indexes of rows that have non-zero numbers in both columns? Or are you trying to construct a new matrix that contains only rows of the original matrix with non-zeros in both columns?
The goal is that I have 208k rows and 2 column, so, I'm just trying to find is there same row of both column has non zero number( like if 100th row of column first and 100th row of column second only have non zero number I want to kepp only these rows and columns).From above matix none of row has non zero number in both column.Moreover I just want to delete the rows, which don't have non zero number in same row of both columns.
Moreover I just want to delete the rows, which don't have non zero number in same row of both columns.
Thats what James gave you. It finds the rows that are not both are not zero and deletes them.
but in your comment like if 100th row of column first and 100th row of column second only have non zero number I want to kepp only these rows and columns). you say you want to keep the ones that both are not zero.
What are you looking for? simply
  1. keep row where both do not equal zero
  2. keep row where either column is zero
  3. keep row as long as both columns are not zero.
  4. other?
Keep row where both don't equal zero. These must be in same rows of both column.
then just modify what James supplied. As you can see he first finds the index where there are non zeros in both.
So we can change it to
ind = sum(A,2)==0;
which will each column to each other. Check if rows equal zero and now we have which ones are equal to zero.
Then we can do A(ind,:)=[] which will get rid of all rows which add up to zero.

Sign in to comment.

matrix = [3145728,0;...
0,3;...
0,3;...
0,3;...
3145728,0;...
0,192;...
0,0;...
384,0;...
48,0;...
3145728,0;...
3145728,0;...
0,384;...
0,3;...
2097152,0;...
0,3;...
3145728,0;...
0,3;...
0,3;...
0,3;...
16777216,0];
Indices = matrix(:,1) ~= 0 | matrix(:,2) ~= 0;
matrix = matrix(Indices,:);

Community Treasure Hunt

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

Start Hunting!