フィルターのクリア

Delete overlaping intervals in matrix

3 ビュー (過去 30 日間)
Filipa Cardoso
Filipa Cardoso 2020 年 9 月 17 日
編集済み: the cyclist 2020 年 9 月 17 日
I have the following matrix:
[100 200; 150 210; 190 300; 300 400; 410 600; 500 700]
I want to delete the intervals that overlap with previous ones.
The final matrix should be:
[100 200; 300 400; 410 600]
How can I achieve this?
Thanks in advance.

採用された回答

the cyclist
the cyclist 2020 年 9 月 17 日
編集済み: the cyclist 2020 年 9 月 17 日
I expect there is a more elegant approach, but here is a straightforward way:
M = [100 200 5;
150 210 7;
205 300 9;
300 400 4;
410 600 3;
500 700 6];
intervalNumber = cumsum([true; M(2:end,1) >= M(1:end-1,2)]);
numberOfIntervals = max(intervalNumber);
output = zeros(numberOfIntervals,size(M,2));
for ni = 1:numberOfIntervals
thisInterval = M(intervalNumber==ni,:);
thisIntervalCol3 = thisInterval(:,3);
output(ni,:) = thisInterval(thisIntervalCol3==max(thisIntervalCol3),:);
end

その他の回答 (1 件)

the cyclist
the cyclist 2020 年 9 月 17 日
編集済み: the cyclist 2020 年 9 月 17 日
I believe this does what you want:
M = [100 200; 150 210; 190 300; 300 400; 410 600; 500 700];
keepRow = M(2:end,1) >= M(1:end-1,2)
output = M([true;keepRow],:)
There are two nuances:
First, it was unclear whether an exact endpoint match should count as overlap. The above algorithm keeps rows if they are equal.
Second, it was unclear whether this needed to be done sequentially, taking into account if the overlap changes based on a prior remove row. So, for example, what should the output be for this input"
M = [100 200; 150 210; 205 300; 300 400; 410 600; 500 700];
Note that I changed M(3,1) from 190 to 205, so that it overlaps row 2, but does not overlap row 1.
  1 件のコメント
Filipa Cardoso
Filipa Cardoso 2020 年 9 月 17 日
編集済み: Filipa Cardoso 2020 年 9 月 17 日
Thank you for the fast reply.
Regarding your questions:
1st- if the endpoint matches it would not count as an overlap.
2nd- thanks to your sugestion, an improvement was made. Therefore the matrix is now:
M = [100 200 5; 150 210 7; 205 300 9; 300 400 4; 410 600 3; 500 700 6];
and if there is an interval overlap the choosen interval should be the one with the highest number in the third column. The end result should be:
M = [205 300 9; 300 400 4; 500 700 6];
Any tips in how to achieve this based on the new data?
Thanks in advance

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

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by