필터 지우기
필터 지우기

how to fill a new vector after a if condition?

조회 수: 2 (최근 30일)
Loren99
Loren99 2022년 6월 20일
편집: Loren99 2022년 9월 15일
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.

채택된 답변

Johan
Johan 2022년 6월 20일
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
Johan
Johan 2022년 6월 28일
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 2022년 6월 28일
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

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spectral Estimation에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by