I need to find equivalent data in two columns with different row numbers in Matlab

Viewed 32

I am trying to write some code in Matlab(2016a).I have a 28x1 matrix library.I have data that I found as a result of the calculations I made. These are in the form of a 595x1 matrix. I want to match each data in the data with the data in the library and output the matching data to the screen. The number of columns is equal, but the number of rows is not.The data I found and the data in the library are not exactly the same. So the data found is 59.2 but the number in the library is 60. I need to be able to match that way. The ismember function didn't work here. It should be something more like min(abs(lib-data)). Then I have to do another operation with each of the matching values (minVal) I find. For example, for each minVal,

if En < minVal - s
  value = (En - (minVal-s)) / s
end

If you could help with this situation, I would greatly appreciate it.

1 Answers

Since your data has no exact matching to some lib, then ismember will not work. Use minimum difference in this case as follows.

lib = rand(28,1) * 10 + 60;
data = rand(595,1) * 10 + 60;

% Find closest values
differences = abs(data' - lib);
[~, ind] = min(differences);
closest = lib(ind);
% display data and the closest from lib
table(data, closest)

Sample output:

     data     closest
    ______    _______

     61.03    61.015 
    64.515    64.394 
    63.019    62.876 
    66.161    66.314 
    63.586    63.648 
      :          :   
    63.871    63.648 
     69.31    69.343 
    64.721    64.901 
    65.245    65.243 
    60.355    60.069 
Related