How can I delete a row based on the value of the previous row?
26 ビュー (過去 30 日間)
古いコメントを表示
I have a file containing one column and a large number of rows. I want to delete any row whose value is less than 2 greater than the previous row (ie if row 1 is 10, and row 2 is 11, then delete row 2. If row 1 is 10, and row 2 is 12.1, then don't delete it). How can I do this? I know how to delete specificied rows with something like
rowstodelete = xxxxxx
A(rowstodelete,:) = [ ];
But I don't know how to figure out which rows need to be deleted (ie what do I need to put in for xxxxx.) I was thinking maybe using logical indexing or if-else statments, but not really sure how. Open to other options too! Thanks in advance!
2 件のコメント
Walter Roberson
2022 年 7 月 31 日
Suppose you have three rows, with values 10, 11.5, 13.3 . The second row is not at least 2 greater than the first. The third row is not at least 2 greater than the second. Do I understand correctly that you would delete the second and the third row both?
Or should the deletion in a sense be carried out "immediately", so that after the 11.5 is deleted, the 13.3 would be compared to the now-previous 10 ?
回答 (5 件)
Bruno Luong
2022 年 7 月 31 日
編集済み: Bruno Luong
2022 年 7 月 31 日
This avoids deletetion which like growsing array wouls kill the performance.
A=[10, 11.5, 13.3 13.5 13.7 17];
keep = false(size(A));
b = -Inf;
for i=1:length(A)
if A(i) >= b
keep(i) = true;
b = A(i) + 2;
end
end
A = A(keep)
0 件のコメント
Walter Roberson
2022 年 7 月 31 日
編集済み: Walter Roberson
2022 年 7 月 31 日
Not a full algorithm, but something to think about
rng(1234)
data = randi(99, 1, 20) %row vector
G = tril(data.'-2 >= data)
pos = sum(cumprod(~G,1),1)+1
find the first 1 in the first column of G; it is at row 2. Entry 2 is the first entry in data that is at least 2 greater than entry 1. Cross check: 62 >= 19+2. Let C = 2 (column 2). pos(1) = 2 -- the required value of the first 1
Look in column C (#2) for the first 1; it is at entry 4. Entry 4 is the first entry in data that is at least 2 greater than entry 2. Cross-check: 78 >= 62+2. Let C = 4 (column 4). pos(2) = 4 -- the required value of the first 1
Look in column C (#4) for the first 1; it is at entry 8. Entry 8 is the first entry in data that is at least 2 greater than entry 4. Cross-check: 80 >= 78+2. Let C = 8 (column 8). pos(4) = 8 -- the required value of the first 1
Look in column C (#8) for the first 1. pos(8) = 9, entry 9 is the first entry, 95 >= 80+2
and so on. The G matrix is the details, the pos vector is the condensed information, the position you need. So you can just read the positions out of pos. pos(1) gives the next index to look at in pos, pos() at that gives the next index, and so on.
When you get an index that is 1 greater than the number of elements in data then you have reached the end, there are no more positions that satisfy the condition.
0 件のコメント
Matt J
2022 年 7 月 31 日
It'll have to be done with a loop,
A=[10, 11.5, 13.3 13.5 13.7 17];
i=2;
while i<=numel(A)
if A(i)<A(i-1)+2
A(i)=[];
else
i=i+1;
end
end
A
0 件のコメント
Bruno Luong
2022 年 7 月 31 日
編集済み: Bruno Luong
2022 年 7 月 31 日
If your data is sorted you could try this
A = 1:0.1:10
A = uniquetol(A,2*(1-eps),'DataScale',1)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!