How to iterate until a point satisfies a certain condition?

조회 수: 3 (최근 30일)
Loren99
Loren99 2022년 6월 21일
댓글: Walter Roberson 2022년 6월 29일
Hi everyone! I need a suggestion for this code. I have this code:
x = [1,2,2,2,1;2,3,2,1,1];
y = [1,1,1,2,2;1,1,2,2,1];
plot(x,y,'-x')
xlim([0,3])
ylim([0,3])
axis equal
Applying this procedure, I obtain that the points that are unique such as (3;1) are removed. The code is this one:
%converts to points with x,y coordinates
points = [x(1,:),x(2,:);y(1,:),y(2,:)];
%initialize test array
repeat_arr = zeros(size(points,2),size(points,2));
for i_points = 1:size(points,2)
%find if a point is repeated excluding itself
repeat_arr(i_points,[1:i_points-1,i_points+1:end]) = all(points(:,i_points)==points(:,[1:i_points-1,i_points+1:end]),1) ;
end
% now we know for each points if it is repeated
temp = any(repeat_arr,1);
%Reverse the segment to point process so we can mask any segment that does not have a repeated point
edge_mask = all([temp(1:end/2); temp(end/2+1:end)]);
%Create X Y array of segment using out mask
x_edges = x(:,edge_mask);
y_edges = y(:,edge_mask);
plot(x_edges,y_edges,'-x')
xlim([0,3])
ylim([0,3])
axis equal
However, my question is: let us imagine that I have also a point of coordinates (2.5;1) like the one in the figure below, and that using this procedure I am able to remove the point (3;1). After this pruning, I would like the procedure to iterate until also the point (2.5;1) (that now is unique) is excluded and therefore execute as long as only the points in common to two or more segments remain. How can I do this in the most general way possible to be able to adapt it to other more complicated situations with more segments? I think that I need a while loop but, how can I set the condition?

채택된 답변

Walter Roberson
Walter Roberson 2022년 6월 21일
편집: Walter Roberson 2022년 6월 24일
You could simplify by using graph() objects https://www.mathworks.com/help/matlab/ref/graph.html
The points to be removed are those with incidence 0 or 1. If you use the adjacency table, the number of nonzero in the row is 0 or 1 for removal
  댓글 수: 5
Walter Roberson
Walter Roberson 2022년 6월 27일
You can rmnode one at a time, but you would have to figure out the order.
Walter Roberson
Walter Roberson 2022년 6월 29일
x = [1,2,2,2,1,3;2,3,2,1,1,3];
y = [1,1,1,2,2,1;1,1,2,2,1,1.5];
xy = [x(:), y(:)];
uxy = unique(xy, 'rows');
[~, idx1] = ismember([x(1,:); y(1,:)].', uxy, 'rows');
[~, idx2] = ismember([x(2,:); y(2,:)].', uxy, 'rows');
G = graph(idx1, idx2);
plot(G, 'XData', uxy(:,1), 'YData', uxy(:,2))
inG = G;
inxy = uxy;
while true
nodes_to_keep_mask = sum(full(adjacency(inG)),2) >= 2;
nodes_to_remove = find(~nodes_to_keep_mask)
if isempty(nodes_to_remove); break; end
inG = rmnode(inG, nodes_to_remove);
inxy = inxy(nodes_to_keep_mask, :);
figure
plot(inG, 'XData', inxy(:,1), 'YData', inxy(:,2))
end
nodes_to_remove = 6
nodes_to_remove = 5
nodes_to_remove = 0×1 empty double column vector

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2022년 6월 21일
You need to code the condition into a while loop.
(I didn't fully understand the condition, so I can't be more specific than that.)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by