Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Sha S
Sha S am 14 Jul. 2015
Kommentiert: Brendan Hamm am 14 Jul. 2015
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.

Antworten (1)

Azzi Abdelmalek
Azzi Abdelmalek am 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 Kommentare
Sha S
Sha S am 14 Jul. 2015
When I tried this I get: "Undefined function or variable 'idx'."
Brendan Hamm
Brendan Hamm am 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,:)=[]

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by