Find intersection point of two datasets
10 views (last 30 days)
Show older comments
Hi,
I have two datasets, each representing velocity as a function of distance (i.e. an array with distance and velocity). The datasets do not share the same distance vectors. I need to find the indices of each data set where the velocities coincide. The data sets are also not the same length. I've tried a few approaches but no luck. Any ideas? Many thanks.
1 Comment
Accepted Answer
Geoff Hayes
on 7 Aug 2014
Adam - if the velocity vectors contain integer values only, then you could use the intersect function and a for loop to determine the indices of elements that coincide. For example,
v1 = [12 10 9 10 13 15 2 3 4];
v2 = [8 7 3 4 12 11 10];
commonVs = intersect(v1,v2);
for k=1:length(commonVs)
v1Idcs = find(v1==commonVs(k));
v2Idcs = find(v2==commonVs(k));
fprintf('common vel=%d v1Idcs={',commonVs(k));
for m=1:length(v1Idcs)
fprintf('%d ', v1Idcs(m));
end
fprintf('} v2Idcs={');
for m=1:length(v2Idcs)
fprintf('%d ', v2Idcs(m));
end
fprintf('}\n');
end
will print out the common velocity and the indices from the two velocity vectors.
If the velocities are floating point values, then you should probably apply some sort of tolerance to determine whether any two velocities are considered to be the same. i.e. is 12.34 the same as 12.343? Yes, if abs(12.34-12.343)<tol, where tol is the tolerance value that you have specified. In this case, I think you would have to first determine which velocities in v1 are considered to be the same, and then map each velocity from v2 into one of these subsets (if possible).
More Answers (0)
See Also
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!