Clear Filters
Clear Filters

How to delete a row if it doesn't follow a pattern?

4 views (last 30 days)
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.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 14 Jul 2015
id=[1 diff(a(:,3))' ]
ii=find(id==0)
for k=1:numel(ii)
idx(k)=ii(k)-1
end
a(idx,:)=[]
  2 Comments
Sha S
Sha S on 14 Jul 2015
When I tried this I get: "Undefined function or variable 'idx'."
Brendan Hamm
Brendan Hamm on 14 Jul 2015
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,:)=[]

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!