Finding a specific pair of points in 2 matrices placed at the same index on both
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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!
0 Commenti
Risposte (2)
Matt Gaidica
il 17 Gen 2021
[row,col] = find(A == 3 & B == 8)
Those will be empty if your condition doesn't exist.
4 Commenti
Image Analyst
il 17 Gen 2021
You'd not be "touching" or changing them, just rounding them
[rows, columns] = find(A == round(usersAValue) & B == round(usersBValue));
Image Analyst
il 17 Gen 2021
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 Commenti
Image Analyst
il 22 Gen 2021
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:
- subtract the matrices from the user values and find where (what single element) the absolute value of the difference is least, or
- 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.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!