%% Average Similar Values in Vectors
% Create two vectors of random numbers and determine which values in A are also members of B, using a tolerance. Specify OutputAllIndices as true to return all of the indices for the elements in B that are within tolerance of the corresponding elements in A.

rng(5)
A = rand(1,15);
B = rand(1,5);
[LIA,LocAllB] = ismembertol(A,B,0.2,'OutputAllIndices',true)

%
% Find the average value of the elements in B that are within tolerance of the value A(13). The cell LocAllB{13} contains all the indices for elements in B that are within tolerance of A(13).

A(13)
allB = B(LocAllB{13})
aveB = mean(allB)

% Copyright 2015 The MathWorks, Inc.