find similar numbers within a matrix
27 views (last 30 days)
Show older comments
Hi. I need to identify the position of a triplet of numbers (P) inside an array (M).
P = [-3.4970900e+01 -2.0289890e+02 -1.7948105e+02];
I am using the 'find' function but the numbers in P is very close to the respective numbers in M.
This does not determine the line of interest for me.
[r,c] = find(M(:,1) == P(1,1) & M(:,2) == P(1,2) & M(:,3) == P(1,3));
That is:
- P(1,1) is equal to or similar to M(?,1);
- P(1,2) is equal to or similar to M(?,2);
- P(1,3) is equal to or similar to M(?,3);
Instead, I determine the line of interest in this way:
[r,c] = find(M(:,1) == P(1,1));
p.s. The variation is only present after the decimal point.
Is there a way to identify within M the (very similar) values present in P? For example by considering a variation of +-0.01 in the numbers in M?
0 Comments
Accepted Answer
Dyuman Joshi
on 11 Jan 2024
Edited: Dyuman Joshi
on 11 Jan 2024
arr = load('M.mat')
M = arr.M;
P = [-3.4970900e+01 -2.0289890e+02 -1.7948105e+02];
tol = 0.01;
%Rows that satisfy the condition for all column elements
idx = all(abs(M-P)<tol, 2)
%Get the output using the row indices
out = M(idx, :)
0 Comments
More Answers (4)
John D'Errico
on 11 Jan 2024
Edited: John D'Errico
on 11 Jan 2024
Lots of good answers posted. I did not see this one.
load M.mat
size(M)
The array M has 61 rows.
P = [-3.4970900e+01 -2.0289890e+02 -1.7948105e+02];
And we want to find an element in M that is close to P.
help knnsearch
[idx,D] = knnsearch(M,P)
It tells us that row 16 of M is the closest one to P, and the distance between the two vectors is not exactly zero, but is very close.
format long g
M(idx,:)
P - M(idx,:)
So it misses only in the third element. If the distance is too arge to be acceptable, then D will tell you that. For example...
[idx,D] = knnsearch(M,[-100 -200 -300])
So that was a miss by a mile.
M(idx,:)
0 Comments
VINAYAK LUHA
on 11 Jan 2024
Hi Alberto,
Try this
[r, ~] = find(abs(M(:,1) - P(1,1)) <= 0.01 & abs(M(:,2) - P(1,2)) <= 0.01 & abs(M(:,3) - P(1,3)) <= 0.01);
0 Comments
Hassaan
on 11 Jan 2024
% Define the matrix M and the vector P
loadedArray = load('M.mat');
M = loadedArray.M;
P = [-3.49799e+01, -2.02899e+02, -1.794815e+02]; % example vector P
% Define the tolerance for similarity
tolerance = 0.01; % For example, 0.01 means we're allowing a difference of up to ±0.01
% Preallocate a logical array for row matches
row_matches = true(size(M, 1), 1);
% Loop through each element in P and update the row_matches array
for i = 1:length(P)
row_matches = row_matches & (abs(M(:, i) - P(i)) < tolerance);
end
% Find the row indices where all elements match P within the tolerance
matching_rows = find(row_matches);
% Display the matching row indices
disp('Matching row indices:');
disp(matching_rows);
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
0 Comments
Steven Lord
on 11 Jan 2024
If you want to find a row of numbers that's close to (but perhaps not exactly equal to, due to roundoff error) another row of numbers, I would use the ismembertol function with the 'ByRows' option.
1 Comment
Dyuman Joshi
on 11 Jan 2024
Edited: Dyuman Joshi
on 16 Feb 2024
How similar is it to the code I've written? And if there is a significant difference, which code is better?
On the surface, it looks very similar -
arr = load('M.mat');
M = arr.M;
P = [-3.4970900e+01 -2.0289890e+02 -1.7948105e+02];
tol = 0.01;
idx = all(abs(M-P)<tol, 2)
out = M(idx, :)
IDX = ismembertol(M, P, tol, 'ByRows', 1)
OUT = M(IDX,:)
See Also
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!