Finding a specific pair of points in 2 matrices placed at the same index on both

Hi all,
Say I have the following matrices:
A= [1 2 3 ; 2 3 6]
B= [6 7 7; 4 8 9]
And the user wants to find where are the values (3,8) (3 on the first matrix and 8 on the second matrix) exist at the same index in both.
Is there an efficient built-in function that will return the output of (2,2), which is the location in both matrixes (A and B) that referes to the user's input?
I need to avoid from accidently finding the 3 on (1,3) in A and the 8 on (2,2) in B - even though they meet the same values of the user's input, they are not indexed on the same places in both matrices.
It is only a toy example, the real matrices A and B are huge so running with loops would be very inefficient, hence my question.
In addition, this code should also work and return the same answer if the user's input will be close enough to (3,8), for instance: (2.9,7.9). How can this be implemented?
Thanks!

Réponses (2)

[row,col] = find(A == 3 & B == 8)
Those will be empty if your condition doesn't exist.

4 commentaires

Thanks, it indeed works.
I updated the requirment at the main post, if you could take a look it'd be highly appreciated :)
Can you just round() each matrix beforehand?
No. the matrices are untouchable.
You'd not be "touching" or changing them, just rounding them
[rows, columns] = find(A == round(usersAValue) & B == round(usersBValue));

Connectez-vous pour commenter.

Try this:
A = [1 2 3 ; 2 3 6]
B = [6 7 7; 4 8 9]
usersAValue = 2.9
usersBValue = 7.9
% Specify how close they can be and still be considered a "match".
tolerance = 0.15;
abs(A - usersAValue)
% Get a matrix with 1's wherever the values are within the tolerance.
matchingMap = abs(A - usersAValue) < tolerance & abs(B - usersBValue) < tolerance
% Find all locations where there is a 1 (where values are within tolerance).
[rows, columns] = find(matchingMap)

3 commentaires

Ran Kagan
Ran Kagan le 22 Jan 2021
Modifié(e) : Ran Kagan le 22 Jan 2021
Thanks a lot!
However, there are 2 additional complications (both may be actually the same issue, just from different point of view), which the code won't be able to deal with (I wasn't really aware of them until now).
1. In my data set, the tolerance is not constant, in a sense that the actual matrices that I need to work with, are formed using a mathematical calculation that doesnt produce a constant difference between the cells' values. Meaning:
r=linspace(0.5, 10, 1000); % here the difference between 2 neighboring values is constant
z=linspace(-10, 10, 360); % here the difference between 2 neighboring values is constant
[R, Z]=meshgrid(r/10,-z/10);
R = R';
Z = Z';
r_sp=sqrt(R.^2+Z.^2);
theta_sp=rad2deg(atan(Z./R))+90;
So the actual difference ("tolerance") that needs to be calculated, is between the input value from the user, and r_sp & theta_sp (correspondingly). Hence, I don't know how can I define a "dynamic" tolerance value (or maybe implement a different way) to find the closest value with a tolerance-difference method.
An example:
  • User's Input: (r=0.25 , theta=90)
On r_sp matrix, the "closest" (of course "close" is a relativistic term, but these values are close enough) sub-matrix is:
0.249839492255565 0.249715236070449
0.250789913138453 0.250666128081148
So one can derive from these values, the required tolernace.
  • However, for a second user's input, say: (r=1, theta=90)
On r_sp, the closest sub-matrix in the matrix is:
1.00124921972504 0.995685178709145
1.00129715839146 0.995733385251227
So the derived tolerance from the first input will not be relevant anymore...
2. Say the tolerance issue was somehow solved, the matchingMap matrix can has more than one "1", in cases where the tolerance wasn't tight enough. In that case, 2 (for instance) values were found to meet the tolerance criteria. This is an unwanted situation, as the user has to get only 1 output, so the following line has to have a minimization of some sort, so the final matrix will contain only one value of "1":
matchingMap = abs(A - usersAValue) < tolerance & abs(B - usersBValue) < tolerance
And on a general note, I don't really need this crazy precision in my calculations. Rounding up all of the values on my code to the 5th decimal point would be adequate enough, but I'm not sure how to do that in Matlab. Maybe this will somehow ease the solution for this issue?
In that line of code, tolerance can either be a scalar that applies to all array elements, or can be an array the same size as the other arrays. In the latter case, you can change tolerance according to position/location if you want.
If you want only a single element or region instead of multiple ones, you can get down to just one element or region. You can either:
  1. subtract the matrices from the user values and find where (what single element) the absolute value of the difference is least, or
  2. if you want a region you can use bwareafilt() to get either the largest or smallest region, or use regionprops to find which region in tolerance has the lowest absolute difference averaged over the region.
Not sure which you want.
"In the latter case, you can change tolerance according to position/location if you want.".
The problem is: I don't know how to map the tolerances of neighboring indices in these huge matrices, as I couldn't figure out what the calculation should be for that, nor whether or not there is a logic in that.
If the difference was constant all along the way, it was perfect. But since this is ain't the case, I'm stuck.
As I demonstrated in the examples, for the case of r=0.25, the smallest difference is ~1.3*10^-4, while for the second case, the smallest difference is ~4.8*10^-5. Setting the tolerance for the second case (which is tigther), and applying it to all of the possible user's input, would be incorrect, as for the case of r=0.25 (and for numerous other cases as well), I won't get an output at all, as the
r_sp-usersValue < tolerance(=~4.8*10^-5)
will yield zero findings.
The fact that this code needs to meet both criteria (provding the closest user's input value & being on the same row and column on both matrices), while being efficient, makes it really hard for me to consdier my next steps. Otherwise it would have been relatively simple.

Connectez-vous pour commenter.

Catégories

Question posée :

le 17 Jan 2021

Modifié(e) :

le 22 Jan 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by