필터 지우기
필터 지우기

Delete row not following pattern

조회 수: 1 (최근 30일)
Sha S
Sha S 2015년 7월 10일
댓글: Image Analyst 2015년 7월 10일
Hi,
I have....
a = [ 2 5 1 0.504;
3 6 2 0.507;
3 4 1 0.589;
9 4 2 0.503;
8 3 1 0.592;
3 2 2 0.203;
9 5 2 0.341;
4 8 1 0.592]
Notice how the 2nd to the last column, column #3, follows a pattern of 1, 2, 1, 2..and so on. The 7th row however has a 2 in the 2nd last column just like the 6th row before...thus does not follow the pattern. I want to delete one of these two rows based on the value in the last column. I want to delete which ever row has a value in the last column further away from the number 0.500.
I would want to delete any row where the 2nd last column does not follow the pattern 1,2,1,2 and when deciding between which row out of the two rows to delete...base it on whichever is further from the number 0.500. Thanks!
  댓글 수: 1
Image Analyst
Image Analyst 2015년 7월 10일
Do you startup the pattern again after deleting the row? In other words, is row 8 correct because you're starting up fresh with the next number in the pattern (1)? Or is it wrong because row 7 should have been a 1 and row 8 should have been a 2. Since row 8 should have been a 2 and it's a 1, it's wrong, unless you're restarting the pattern after deleting row 7.

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2015년 7월 10일
편집: James Tursa 2015년 7월 10일
Assuming the pattern in the next-to-last column must be 1-2-1-2-...etc exactly
[m,n] = size(a);
n1 = n - 1;
expected = 1; % initialize expected value in 1st row
x = false(m,1); % initialize the deletion flag array
keep = 0; % initialize row to keep when marking for deletion
v = 0.5; % discriminator value for deciding which line to delete
for k=1:m
if( a(k,n1) ~= expected ) % if not as expected, mark one line for deletion
if( keep == 0 || abs(a(k,n)-v) > abs(a(keep,n)-v) )
x(k) = true; % mark current line for deletion
else
x(keep) = true; % mark previous line for deletion
keep = k;
end
else
expected = 3 - expected; % if as expected, update expected
keep = k;
end
end
a(x,:) = []; % delete the unexpected pattern rows
If the first line can start with a 2 rather than a 1, change this line:
expected = a(1,n1); % initialize expected value in 1st row

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 10일
편집: Azzi Abdelmalek 2015년 7월 10일
a = [ 2 5 2 0.504; 3 6 2 0.507; 3 4 1 0.589; 9 4 2 0.503; 8 3 1 0.592; 3 2 2 0.203; 9 5 2 0.341; 4 8 1 0.592]
c3=a(:,3)
c4=a(:,4)
id=[1 diff(c3)' ]
ii=find(id==0)
for k=1:numel(ii)
[~,jd]=min(c4(ii(k)-1:ii(k)))
idx(k)=ii(k)+jd-2
end
a(idx,:)=[]

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by