How to delete a row if it doesn't follow a pattern?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I have... a = [ 2 5 1; 3 6 2; 3 4 1; 9 4 2; 8 3 1; 3 2 2; 9 5 2; 4 8 1]
Notice how the last column follows a pattern of 1, 2,1,2..and so on. The 6th & 7th row both have a 2 in the last column...thus does not follow the pattern. How would I delete the 6th row and any other row if it does not follow the 1,2,1,2 pattern of the last column?
*If there are repeated numbers in the last column I would want to keep the last row with the repeated number and delete the others Thanks.
0 个评论
回答(1 个)
Azzi Abdelmalek
2015-7-14
You can adapt the answer of your previous question http://www.mathworks.com/matlabcentral/answers/229434-delete-row-not-following-pattern#comment_297496
id=[1 diff(a(:,3))' ]
ii=find(id==0)
for k=1:numel(ii)
idx(k)=ii(k)-1
end
a(idx,:)=[]
2 个评论
Brendan Hamm
2015-7-14
That must mean there are no places which satisfy:
id == 0
When this is the case the ii will be empty, the loop never entered and finally idx will not be initialized:
id = [1 2 5];
ii = find(id == 0)
ii =
[]
numel(ii)
ans =
0
So just initialize idx outside of the loop to the empty array
id=[1 diff(a(:,3))' ]
ii=find(id==0)
idx = []; % ADD THIS LINE
for k=1:numel(ii)
idx(k)=ii(k)-1
end
a(idx,:)=[]
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!