Search code examples
arraysmatlabcomparison

Comparing two arrays and extracting corresponding values of 2nd array to new array MATLAB


I have 2 different arrays.

Array 1: Distance1 = [1 km distance]

Size of Distance1 = 700x1

Array 2: Data = 2 columns [1st column Distance2 = 1km, 2nd column Speed limit]

Size of Data = 1000x2

I want to make a new array where program go to distance1 select all distance points compare it to distance2 points (first column) in Data and put corresponding Speedlimit (2nd Column in Data) values in new array. At the end the new array size should be equal to Distance1.

I have tried this code but could not make it work:

Distance1;
Distance2;
Speedlimit;
Data = [Distance2, Speedlimit]
Result = Distance1(dsearchn(Data(:,1),Distance2),2)

Solution

  • I think you've mixed up a few of your variable names in your last line. I believe you should be doing this:

    Result = Data(dsearchn(Data(:,1), Distance1), 2);