Find specific rows of matrix that are subsets of other rows
2 views (last 30 days)
Show older comments
Hello to all,
I am facing the following problem:
Let us assume that we have a matrix A = [2 10;15 20;50 60;65 70;61 64;16 18;66 69;67 68].
The elements of A(:,1) correspond to start times and the elements of A(:,2) to the end times of something. Thus, the elements of each row correspond to a closed set of values, for example row 1 corresponds to set [2 10].
I would like to find rows that are subsets of other rows and delete them. Specifically after the application of the requested procedure new matrix A_2 should be A_2 = [2 10;15 20;50 60;65 70;61 64], because last three rows of matrix A corresponded to subsets of rows 2 and 4.
I hope that i did explain the problem sufficiently.
2 Comments
Jan
on 28 Feb 2013
Does subset mean inclusive or exclusive the edge? In other word, is [67,70] a subset of [65,70]?
Accepted Answer
Jan
on 28 Feb 2013
A = [2 10;15 20;50 60;65 70;61 64;16 18;66 69;67 68];
A1 = A(:, 1);
A2 = A(:, 2);
inside1 = bsxfun(@ge, A1, A1.') & bsxfun(@le, A1, A2.');
inside2 = bsxfun(@ge, A2, A1.') & bsxfun(@le, A2, A2.');
inside = inside1 & inside2;
inside(1:length(A1)+1:end) = false; % Clear main diagonal
toDelete = any(inside, 2);
A(toDelete, :) = [];
0 Comments
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!