# How to populate a new vector by extracting only certain elements from another vector?

4 views (last 30 days)
Loren99 on 15 Jun 2022
Commented: Image Analyst on 15 Jun 2022
Hi everyone!
I would like to obtain two vectors new_vrx (with size 2xn) and new_vry (with size 2xn).
new_vrx(1,:) is the x coordinate of the initial point of the segment.
new_vrx(2,:) is the x coordinate of the final point of the same segment.
new_vry(1,:) is the y coordinate of the initial point of the segment.
new_vry(2,:) is the y coordinate of the final point of the same segment.
I would like to keep only those segments for which both the starting and ending point coordinates have values Vq1 = 0 and Vq2 = 0. Here my lines of code (I have attached also a .zip file containing the variables M,vrx,vry,vq1 and Vq2):
[Xpix, Ypix] = meshgrid(1:321,1:241);
%vrx(1,:) is the x coordinate of the initial point of the segment
%vrx(2,:) is the x coordinate of the final point of the segment
%vry(1,:) is the y coordinate of the initial point of the segment
%vry(2,:) is the y coordinate of the final point of the segment
Vq1 = interp2(Xpix,Ypix,double(M),vrx(1,:),vry(1,:));
Vq2 = interp2(Xpix,Ypix,double(M),vrx(2,:),vry(2,:));
At the end I would like to plot the new vectors obtained after this pruning, in this way:
plot(new_vrx,new_vry,'b-')
Could you help me? Thanks in advance

Hi Loren,
if understood your question correctly, this should do what you want:
[Xpix, Ypix] = meshgrid(1:321,1:241);
Vq1 = interp2(Xpix,Ypix,double(M),vrx(1,:),vry(1,:));
Vq2 = interp2(Xpix,Ypix,double(M),vrx(2,:),vry(2,:));
new_vrx = vrx(1,Vq1==0);
new_vry = vry(1,Vq1==0);
figure;plot(new_vrx,new_vry,'b-') and
new_vrx = vrx(2,Vq2==0);
new_vry = vry(2,Vq2==0);
figure;plot(new_vrx,new_vry,'b-') Image Analyst on 15 Jun 2022
"it doesn't answer my question." <== Well now you've accepted the answer so people will (rightfully?) assume that you have since figured it out.