Removing certain Rows from a matrix
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Paulo Silva
2011-9-3
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 个评论
Paulo Silva
2011-9-3
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 :)
Andrei Bobrov
2011-9-3
Hi friends! My variant
n = reshape(permute(reshape(m,size(m,1),[],2),[1 3 2]),[],2);
out = n(any(n,2),:);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!