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

1 回表示 (過去 30 日間)
Sha S
Sha S 2015 年 7 月 8 日
コメント済み: Sha S 2015 年 7 月 15 日
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.
  2 件のコメント
James Tursa
James Tursa 2015 年 7 月 8 日
Is the pattern always two different numbers alternating? Or could it be something else?
Sha S
Sha S 2015 年 7 月 8 日
The pattern is always the numbers 1 and 2 alternating.

サインインしてコメントする。

採用された回答

Ashmil Mohammed
Ashmil Mohammed 2015 年 7 月 8 日
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
  1 件のコメント
Sha S
Sha S 2015 年 7 月 10 日
Hi Ashmil, I have adjusted my data a bit...
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 last column 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!

サインインしてコメントする。

その他の回答 (2 件)

bio lim
bio lim 2015 年 7 月 8 日
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];
% First lets remove 2's in a row
ii = 1:2:length(a);
b = a(ii,end)~=1;
a(2*find(b==1)-1, :) = [];
% Second lets remove 1's in a row
jj = 2:2:length(a);
c = a(jj,end)~=2;
a(2*find(c==1)-1,:) = [];
Output is
a =
2 5 1
3 6 2
3 4 1
9 4 2
8 3 1
3 2 2
4 8 1

James Tursa
James Tursa 2015 年 7 月 8 日
編集済み: James Tursa 2015 年 7 月 8 日
Assuming the pattern in the last column must be 1-2-1-2-...etc exactly
[m,n] = size(a);
expected = 1; % initialize expected value in 1st row
x = false(m,1); % initialize the deletion flag array
for k=1:m
if( a(k,n) ~= expected )
x(k) = true; % if not as expected, mark for deletion
else
expected = 3 - expected; % if as expected, update expected
end
end
a(x,:) = []; % delete the unexpected pattern rows
  2 件のコメント
James Tursa
James Tursa 2015 年 7 月 8 日
編集済み: James Tursa 2015 年 7 月 8 日
Note: My answer and coffee's answer differ in how they treat a matrix that starts with a 2 in the last column of row 1 (and potentially the adjacent rows). My answer will delete all beginning rows until a 1 is found. coffee's answer does something different. If you want to allow beginning with a 1 or a 2, alter the expected initialization line as follows (assumes a(1,n) has to be either a 1 or a 2 and can't be anything else):
expected = a(1,n); % initialize expected value in 1st row
Sha S
Sha S 2015 年 7 月 15 日
How would I change this if I wanted to delete the 6th row rather than the 7th row?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by