Find value in a matrix
Show older comments
Hello! I wanted to check if a value exists inside a matrix without the use of a loop. To be more spesific I have a matrix, D, which is 50x50 and I want to check if a variable A, is in D. I used ismember(A,D) but it doesn't work. This function works only if D is an array? What can I do to look through a matrix without a loop? Thank you in advance.
2 Comments
Stephen23
on 9 Jan 2020
"I used ismember(A,D) but it doesn't work."
Taking a guess: you have floating point number issues.
"What can I do to look through a matrix without a loop?"
ismembertol
Adam Danz
on 9 Jan 2020
I second the ismembertol() suggestion.
A lower-level approach would be to simply subtract the value from each element of the matrix and look if any of the results are very close to zero.
% M is the matrix
% v is the scalar value
[row,col] = find(abs(M-v) < 0.000001)
row and col will be empty if there are no "matches". Otherwise they will contain the row and column numbers of all "matches".
Accepted Answer
More Answers (0)
Categories
Find more on Resizing and Reshaping Matrices 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!