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)
Show older comments
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.
1 Comment
Guillaume
on 11 Dec 2017
A vector is a matrix where all dimensions but one is of size 1. Therefore 2000 x 5 is not a vector.
Answers (2)
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
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.
Stephen23
on 11 Dec 2017
Edited: Stephen23
on 11 Dec 2017
>> 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
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!