Check if there are repeated elements in a matrix
1 次查看(过去 30 天)
显示 更早的评论
Dear all,
I have a set of lines/polylines represented in a matrix coord 380x4 (attached with this post), first and second columns representing X and Y coordinates respectively, and 4th column representing ID of a line/polyline. I want to check the continuity of this set. To do that, I need to check any point within one line/polyline have another point with the same coordinates on another line/polyline. Now, let me explain what I mean. For example, if we refer to the matrix coord attached with this thread, first ten rows (1:10) have the ID of 42 meaning that this polyline consists of 10 points connected one after another. The next polyline is presented from rows 11 to 17 having ID of 43. Now we can see that
coord(10,1) == coord(11,1) & coord(10,2) == coord(11,2)
which means that these two polylines are connected. The program is expected to check every set of points within each line/polyline and identify where there is a gap if it exists. For example, if the polyline with ID of 56 did not have any points present in other polylines, it would mean that this polyline is disconnected. And, if the line/polyline is disconnected, I want to see which ones are disconnected by showing their indexes.
Thanks to the answer by Guillaume, I realised that the approach needs to be changed. So I am not happy if there are more than two connected poly(lines) are disconnected. What I want to check is the continuity between the first line and the rest of the lines.
Any ideas guys please? I am stuck now.
Thank you
0 个评论
回答(1 个)
Guillaume
2018-3-15
The following should work so long as for a single ID the same point is not repeated. Also it won't detect if you have two disconnected groups of connected lines. Finally, the points coordinates have to be exactly equal down to the last significant digits:
[~, ~, pointid] = unique(coords(:, [1 2]), 'rows'); %assign same id to identical points
idrepeat = accumarray(pointid, 1); %compute histogram of each id
pointrepeat = idrepeat(pointid); %how many times each point of coords is found in coords
[label, ~, labelid] = unique(coords(:, 4));
labelconnections = accumarray(labelid, pointrepeat, [], @max) %for each label get the max of pointrepeat. A label has at least that many connections
Any label for which labelconnections is 1 does not have any of its point found in another label.
4 个评论
Guillaume
2018-3-16
编辑:Guillaume
2018-3-16
"Is there any other way to check it?"
There are many different algorithms you could come up with. What is wrong with the one I wrote?
"Because after all I want that the disconnected line or set of lines will connect to the closest point of a line(s) which is/are connected"
Sorry, I don't understand that.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Feature Detection and Extraction 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!