how to fill a new vector after a if condition?

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.

 Risposta accettata

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 Commenti

Loren99
Loren99 il 21 Giu 2022
Modificato: Loren99 il 21 Giu 2022
@Johan Pelloux-Prayer thanks for your answer. A last question. Using your procedure, I obtain a first-level pruning: for example if I see your figure, let us imagine that we have also a point of coordinates (2.5;1) and that using your procedure we are able to prune the segment between (2.5;1) and (3;1).
I would like the procedure to iterate until this second segment joining (2.5;1) with (2;1) is also excluded and therefore 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 my code?
You could put the process in a function and repeat it until the output and the inputs are the same
x = [1,2,2.5,2,2,1;2,2.5,3,2,1,1];
y = [1,1,1,1,2,2;1,1,1,2,2,1];
x_temp = x;
y_temp = y;
condition = 1;
escape = 1;
while condition
[x_edges,y_edges,condition] = pruning(x_temp,y_temp);
x_temp = x_edges;
y_temp = y_edges;
escape = escape +1; %prevent infinite loops
if escape > 100
break
end
end
subplot(1,2,1)
plot(x,y,'-x')
xlim([0,3])
ylim([0,3])
axis equal
subplot(1,2,2)
plot(x_edges,y_edges,'-x')
xlim([0,3])
ylim([0,3])
axis equal
function [x_edges,y_edges,pruning_left] = pruning(x,y)
%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));
%initialize end check for pruning
pruning_left = 1;
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);
% if all temp are 1 then all points are at least part of two segment
if all(temp)
pruning_left = 0;
end
%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);
end
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);
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Linear Algebra in Centro assistenza e File Exchange

Prodotti

Release

R2021b

Richiesto:

il 20 Giu 2022

Modificato:

il 15 Set 2022

Community Treasure Hunt

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

Start Hunting!

Translated by