Removing certain Rows from a matrix

I am producing a number of columns, which are pairs of x and y coords. I.e, the following code shows two sets of x&y coordinates. I need to plot these coordinates, but somehow need to remove all of the rows where two 0's are present.
I was wondering if anyone could help me with a way to extract a set of 2 columns at a time, and only return the coordinates up to the point where the 0 0 starts.
5.0000 5.0000 5.0000 5.0000
5.0000 5.3626 2.4232 5.0000
3.5471 5.3626 0.8831 5.0000
2.9189 5.3626 0.8831 6.1958
2.9189 3.4729 0.8831 6.2884
2.9189 5.3171 0 6.2884
3.0675 5.3171 0 0
3.6450 5.3171 0 0
3.6450 5.9336 0 0
3.6450 6.5008 0 0
3.6450 8.4064 0 0
5.2608 8.4064 0 0
5.2608 6.9090 0 0
5.2608 5.5735 0 0
5.2608 7.0445 0 0
2.6390 7.0445 0 0
3.2644 7.0445 0 0
3.2644 5.1236 0 0
3.8823 5.1236 0 0
3.6361 5.1236 0 0
3.6361 5.5497 0 0
3.6361 7.4126 0 0
3.6361 7.2564 0 0
1.4501 7.2564 0 0
1.4501 7.0662 0 0
0 7.0662 0 0
The data is produced from a for loop, and the loop does not always run for the same amount of time steps.

Answers (2)

n=[m(1:size(m,1),[1 2]); m(1:size(m,1),[3 4])]; %two columns
n=n(any(~ismember(n(:,[1 2]),[0 0]),2),:) %one column not 0
%n=n(all(~ismember(n(:,[1 2]),[0 0]),2),:) %both columns not 0
in response to Daniel comments, one way to have 4 columns again
n=[n;nan(1,2)] %you have to add nan values if size(n,1) isn't even
%there was one row missing, now size(n,1) is even
n=[n(1:size(n)/2,:) n((size(n)/2+1):end,:)]

13 Comments

this is for when we know for example the two last columns are included zero , but if we do not know , then?
I don't understand the question, please rephrase it
my mean for example we have a matrix that we do not know exactly which columns are included of zeros (more than half of the rows)
then we want to recognize them and or remove them,
Mohammad please provide me with a simple example of a matrix where that happens and what's the resulting matrix after doing what you need
X=[2.47 0 133.717 0 0 57 0 17 20 4.554 5.361
2.377 0 124.799 0 0 34 0 10 19 4.489 5.293
2.377 0 124.799 0 34 0 10 0 19 4.489 5.293
2.398 0 148.023 0 77 0 23 0 22 4.673 5.485
2.445 0 136.51 0 0 40 0 12 20 4.533 5.333
2.676 0 138.08 0 0 46 0 14 20 4.533 5.323
2.483 0 129.093 0 0 20 0 6 19 4.466 5.252
2.514 0 121.675 0 0 0 0 0 18 4.394 5.165
2.445 0 136.51 0 40 0 12 0 20 4.533 5.333
2.465 0 160.208 0 89 0 27 0 23 4.71 5.517
2.633 0 145.498 0 20 32 13 18 21 4.595 5.398
2.445 0 136.51 0 0 32 0 18 20 4.533 5.333
]
and then i want to know which columns have a lot of zero( for example half of the number of row)
for example in this case columns 2, 4, 5 7,
zerolike_columns = find(mean(X == 0) >= 1/2);
walter Thanks, it works, I have another question ,
when I have a binary matrix (zero and one) I want to recognize in each rows which cell has value 1 , could you please also tell me that
like this Matrix
X=[0 0 0
0 0 0
0 0 0
1 0 0
0 0 1
0 1 1
1 0 0
0 1 0
0 0 1
1 0 0
]
@Paulo Silva
Thanks, the %one column not zero is almost the solution I am after. But because each set of two columns represents a set of x and y coordinates for a seperate human (walking from an origin point in my matlab program) I still need to have 6 seperate columns if i have 3 humans, (i.e 2 columns per human walking). So Is there any way to re-seperate the matrix you have in the second row of your answers, so that it returns something similar to the original?
Thanks a lot!
Another solution for the previous question
%m is the array, I prefer to use m instead of X for the array name
c=arrayfun(@(n)sum(m(:,n)==0),1:size(m,2)); %count the zeros in each column
z=5; %number of zeros
find(c>z) %find what columns have more zeros than the number you selected (z)
thanks Paulo
for the second question i wrote this, but does not work , because i want to know in each row which cells are included of one
[m,n]=size(X)
for i=1:m
onevlue=find(X==1)
end
solution for Mohammad second question
[row,col]=find(X==1);
result=sortrows([row col])
%first column is the rows where the ones are and second column indicates the %column of the ones, also works for rows with multiple ones
I hope to have answered all questions the correct way, Mohammad please create new questions for your own problems, don't hijack other people questions, that makes it hard for us to help, must go now, have a nice day :)
thanks, have a nice day

Sign in to comment.

Hi friends! My variant
n = reshape(permute(reshape(m,size(m,1),[],2),[1 3 2]),[],2);
out = n(any(n,2),:);

Asked:

on 3 Sep 2011

Community Treasure Hunt

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

Start Hunting!