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.

回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2015-7-14
id=[1 diff(a(:,3))' ]
ii=find(id==0)
for k=1:numel(ii)
idx(k)=ii(k)-1
end
a(idx,:)=[]
  2 个评论
Sha S
Sha S 2015-7-14
When I tried this I get: "Undefined function or variable 'idx'."
Brendan Hamm
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by