MATLAB: How to delete a row if it doesn’t follow a pattern

delete rowpattern

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 7th row however has a 2 in the last column just like the 6th row before...thus does not follow the pattern.
How would I delete the 7th row and any other row if it does not follow the 1,2,1,2 pattern of the last column?
Thanks.

Best Answer

Try out this code :
p=size(m,1);%m is your matrix
for i=1:p
if mod(i,2)==1 && m(i,size(m,2))~=mod(i,2)
m(i,:)=[];
p=p-1;
elseif mod(i,2)==0 && m(i,size(m,2))~=(mod(i,2)+2)
m(i,:)=[];
p=p-1;
end
end