필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Sha S
Sha S 2015년 7월 14일
댓글: Brendan Hamm 2015년 7월 14일
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에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by