필터 지우기
필터 지우기

Check if there are repeated elements in a matrix

조회 수: 3 (최근 30일)
Yesbol
Yesbol 2018년 3월 15일
편집: Guillaume 2018년 3월 16일
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

답변 (1개)

Guillaume
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
Yesbol
Yesbol 2018년 3월 16일
Thanks, Guillaume. Yes, you're right, by continuity I mean that polyline can connect through any point. Is there any other way to check it? 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
Thank you
Guillaume
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 CenterFile Exchange에서 Feature Detection and Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by