1

I have two column vectors of unequal lengths, calib (75 elements) and nu (1436 elements). I need to find the elements in nu that match the elements in calib to within a tolerance of 0.001, then perform operations on the resulting vector. All the elements are numbers not strings.

I thought intersect would be the way to go, but I don't see a way to introduce a tolerance. Also using a for loop with an absolute difference statement didn't seem to work.

Any help would be much appreciated.

Leif Wickland
  • 3,693
  • 26
  • 43
bene
  • 13
  • 3
  • Check my previous question, that might be helpful: http://stackoverflow.com/questions/2142826/mapping-2-vectors-help-to-vectorize – yuk Mar 04 '13 at 21:13
  • That's an interesting idea. I'll have to try it and play around with it a bit. – bene Mar 05 '13 at 21:26

3 Answers3

3

you can round one of you vectors to show only 0.001 accuracy and then use ismember. Or just use ismemberf - Floating-point ISMEMBER (i.e., with round-off tolerance) from the FEX.

bla
  • 25,846
  • 10
  • 70
  • 101
1

I have another Matlabic (=matrix) way for this. I don't know how computationaly efficient this is, but Matlab should be good with matrices.

I'm assuming size(nu) = [75,1] and size(calib) = [1436,1]. The first step is making two big 1436x75 matrices (note the transpose):

calib_m = repmat(calib',size(nu))
nu_m = repmat(nu,size(calib'))

Then you can make another matrix 1436x75 that is the absolute difference of the above:

diff_m = abs(calib_m - nu_m)

Now find the smallest element in each column:

min_m = min(diff_m)

Then you can introduce your threshold and do a logical indexing (which should be fast):

ok_calib_elements = calib(min_m < THRESHOLD)

PS: I don't have Matlab here, so the code is not tested

Dedek Mraz
  • 814
  • 7
  • 14
0

Thanks for the help. The matrix math got me where I needed to go. I don't know if there is a better way to do this, but here is my code that works:

% set tolerance for finding equal values in the data
find_tol = 1E-3; 

% make matrices of the data and clibration vectors
nu_m = repmat(nu',size(calib));
calib_m = repmat(calib,size(nu'));

% subtract the matrices
diff_m = calib_m - nu_m;

% find the minimum of each column within the tolerance and compute the
% average shift
find_min_nu = find(diff_m < find_tol & diff_m > -find_tol);
min_nu = diff_m(find_min_nu);
shift_nu = mean(min_nu);

% output calibrated values
calib_nu = nu + shift_nu;
bene
  • 13
  • 3