2
votes

I have two vectors, one 12x1 and the other 2430x1. I want to take each element of the first vector and find which is the immediately after the matching element in the second vector, saving it in a third vector.

Example:

V1 = [1, 2, 3]
V2 = [1.2, 2.3, 2.5, 3.3, 3.4, 3.7 ......]

I would like to have the value for each V1 element immediately after each corresponding V2 element. I should then get

V3 = [1.2, 2.3, 3.3]
2
Is V2 alway sorted in accending order?EBH
@EBH Yes, but there may not be any common elements between the two carriersAlice

2 Answers

2
votes

You can use interp1 setting the interpolation method as 'next':

%remove common elements
V3 = setdiff(V2,V1);
%get the next elements
result = interp1(V3,V3, V1,'next','extrap')

Thanks to @SardarUsama for his clarification and testing.

0
votes

Sounds like you want to use the V1 vector as indices to go through V2... try this maybe:

For i =1:length (V1)
V3 (i) = V2 (V1 (i));
End

I'm not exactly sure if this is what you're asking for though...