Compare Matrices within tolerance range
Show older comments
Hi. I have a matrix A that is 8323 by 3 and matrix B that is 3 by 3. Elements in both these matrices have decimals. I want to find rows in A that is within 5% tolerance of rows in B. I am pretty sure you have to use for loop and if statements to find it. However, i am not sure what to write. I made 2 matrices C and D of same size as B since i calculated the max and min values that correspond to B values.
for y=1:3
for x=1:n1
if Points1(:,y)<=none1(x,y) && Points1(:,y)>=none3(x,y)
%not sure what to write
And as always, thanks.
5 Comments
You mention matrices A and B, but they do not appear in your code. Also, it is not clear what it means for 2 rows to be 5% apart. Do you mean each pair of corresponding scalar elements in the compared rows differs by 5%, or do you compare them with l2 or some other norm?
Matt J
on 13 Jun 2015
Anna Lin commented:
pt1=[52.537 -39.937 -66.162]; %all points are in mm units
pt2=[-17.518 -66.453 -57.536];
pt3=[87.653 -72.022 -99.625];
points3=[pt1; pt2; pt3]
A=points3
Points1=B
i calculated +/- 5% of each value in matrix A and want to find the elements in matrix B that are within that range of numbers. matrix C is higher range(+5%) of matrix A and matrix D is the lower range(-5%) of matrix A Basically i want to find the rows in matrix B that matches to each row in matrix A. However, the values in both matrices are not integers and probably won't have exact numbers since the precision is to the thousandth place after the decimal. Therefore i had to give a range of numbers that is close enough
Jan
on 13 Jun 2015
@Anna Lin: doubles do not have a precision of thousands of decimals, but only about 16 digits.
Image Analyst
on 13 Jun 2015
I guess she meant "the thousands place", which would be the third place after the decimal. I wish it were thousands of decimal places - maybe in another 50 years it will be.
@Anna Lin: points3, Points1, A, B, C, D, none1, none3 - the bunch of variables is at least confusing. What is the wanted result? Do you want a [8323 x 1] logical vector with a true, if the corresponding row of A is inside one of the three intervals defined by the rows 0.09*B and 1.05*B? Or do you want a [8823 x 3] array?
Answers (2)
Guillaume
on 13 Jun 2015
iswithintol = ismembertol(A, B, 0.05, 'ByRows', true)
will find rows of A that differ from the rows of B by no more than 5% of the max of A and B. There are options to change the tolerance criteria.
1 Comment
Paul Hoffrichter
on 10 Dec 2020
I was looking up a solution for this problem when I saw ismembertol above. But consider:
A = [1 2 3];
B = [3 1 2];
In my case, and possibly in the OP, A and B are out of tolerance since I want each element of A to be within tolerance of the corresponding element of B. But we get:
ismembertol(A,B)
ans =
1×3 logical array
1 1 1
Paul Hoffrichter
on 10 Dec 2020
Edited: Paul Hoffrichter
on 10 Dec 2020
For a tolerance match between corresponding elements of the rows, start off with this:
a = a(:);
b = b(:);
%% Check for equivalence of each element, within the tolerance
yn = abs(a - b) <= eps(max(abs(a), abs(b)));
yn = all(yn); % scalar logical output
Next step is to modify above to include desired tolerance.
Categories
Find more on Logical 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!