how to fill a new vector after a if condition?

5 views (last 30 days)
Hi everyone! I have two vectors new_vrx and new_vry (each one has size 2x941) where:
I would like to create new vectors: x_edges (size 2 x n) and y_edges (size 2 x n) mantaining only those coordinate points (new_vrx, new_vry) that belong to more than one segment.

Accepted Answer

Johan
Johan on 20 Jun 2022
I would convert your segment coordinates to points, this way points that are not unique would show that the segment is not an end point.
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
%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
  4 Comments
Johan
Johan on 28 Jun 2022
Have you tried running the pruning loop with segments that are in your limiting box ?
x_temp = new_vrx(in);
y_temp = new_vry(in);
Johan
Johan on 28 Jun 2022
As you suggest the issue comes from segment that are both in and out of the box.
Doing this seems to work with your data:
x_temp = new_vrx(:,all(in));
y_temp = new_vry(:,all(in));
It's essentially the same thing that you did but in array format. If think your code didn't work because you checked on all values of in instead of the k value:
for k = 1:length(new_vrx)
if all(in(:,k)) %initial and final point of the same segment are inside the rectangle, so they could be eventually pruned
new_new_vrx = [new_new_vrx, new_vrx(k,:)];
new_new_vry = [new_new_vry, new_vry(k,:)];
end
end

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!