Can somebody help me in removing 10 rows above and below, if the difference between two rows in first column is >2

1 view (last 30 days)
I have vector 2000*5 Example
0 2 3 6 5
2 5 6 9 4
4 5 8 9 9
10 3 6 9 7
12 6 9 8 1
14 5 6 9 3
and so on
I want to remove 10 row above and below when the difference between two rows in first column is > 2.

Answers (2)

Guillaume
Guillaume on 11 Dec 2017
"when the difference between two rows in first column is > 2" I'm assuming you mean the absolute difference between consecutive rows. If so,
rowstoremove = max(min(find(abs(diff(yourmatrix(:, 1))) > 2) + (-10:10), size(yourmatrix, 1)), 1);
yourmatrix(rowstoremove, :) = []
You may need to change the (-10:10) to reflect what you really want, depending if it's 10 rows above the first of the two consecutive rows and 10 rows after the second consecutive rows (in which case, it may be -10:11).
  4 Comments
Kurni Eswar
Kurni Eswar on 11 Dec 2017
x = [true;diff(Difference(:,1))<=2]; idy = [true;true;x(1:end-2)]&[true;x(1:end-1)]&x&[x(2:end);true]&[x(3:end);true;true]; y=Difference(idy,:);
I got it in mathworks but it was helping me two remove 2 rows only I want to remove 5 row
Guillaume
Guillaume on 11 Dec 2017
Edited: Guillaume on 11 Dec 2017
Shows this error Error using + Matrix dimensions must agree.
Then you're using an old version of matlab (before R2016b). Mention that when you ask questions.
In older versions of matlab:
rowstoremove = max(min(bsxfun(@plus, find(abs(diff(yourmatrix(:, 1))) > 2), -10:10), size(yourmatrix, 1)), 1);
But it looks like you've already asked the same question in another post. Do not duplicate questions. It wastes everybody's time.

Sign in to comment.


Stephen23
Stephen23 on 11 Dec 2017
Edited: Stephen23 on 11 Dec 2017
Using exactly the code that I gave in a comment to your earlier question:
>> mat = csvread('Demo.csv');
>> idx = [false;diff(mat(:,1))>2];
>> N = 10; % number of rows before/after
>> idy = ~conv(idx,ones(1+2*N,1),'same');
>> mat(idy,:)
ans =
0.00000 506.70000 400.00000 473.10000 336.50000
2.00000 512.80000 400.30000 478.90000 335.50000
4.00000 517.10000 400.80000 483.20000 335.30000
6.00000 518.20000 401.50000 484.80000 335.70000
8.00000 517.70000 401.80000 485.90000 336.30000
66.00000 512.60000 386.10000 483.40000 330.00000
68.00000 512.50000 386.10000 483.10000 330.20000
70.00000 512.80000 386.10000 483.60000 330.60000
122.00000 513.80000 384.40000 483.20000 327.90000

Tags

Community Treasure Hunt

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

Start Hunting!