# Find value in a matrix

1 ビュー (過去 30 日間)
Zenia Askar 2020 年 1 月 9 日
コメント済み: James Browne 2020 年 1 月 10 日
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 件のコメント

Stephen Cobeldick 2020 年 1 月 9 日
"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 2020 年 1 月 9 日
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".

サインインしてコメントする。

### 採用された回答

James Browne 2020 年 1 月 10 日
Why not use a loop? You can create one with very little code and you can easily modify it to do whatever you want. For example, I wrote the following code, which not only searches for a value in a matrix, D, of arbitrary size and sets a boolean variable to TRUE, if the value os found in D, but also reports the number of times it is found. You can also add lots of things, easily, such as a tolerence range or something...
%Create a 50x50 matrix of random integers between 1 and 10
D = randi(10,50);
%Determine the dimensions of the matrix D
NRows = size(D,1);
NCols = size(D,2);
%Value to be searched for in the matrix D
CompareValue = 5;
%Variable which indicates whether or not "CompareValue" was found in the
%matrix D
ValueFound = false;
%Number of times that CompareValue was found in the matrix D
NumTimesFound = 0;
%Search the matrix D to see if the value in "CompareValue" is present
for i = 1:NRows
for j = 1:NCols
if D(i,j) == CompareValue
ValueFound = true;
NumTimesFound = NumTimesFound + 1;
end
end
end
fprintf('The value %4.2f was found in the matrix D %i times', CompareValue, NumTimesFound)

#### 2 件のコメント

Adam Danz 2020 年 1 月 10 日
The loops are unnecessary and can be replaced with a cleaner and faster vectorization:
matches = D==CompareValue;
NumTimesFound = sum(matches(:));
fprintf('The value %4.2f was found in the matrix D %i times \n', CompareValue, NumTimesFound)
Also, don't forget the \n at the end of the fprintf so a new line is started.
But this method, and the nested loops, will only work if you're looking for exact matches which usually isn't the case. As Stephen Cobeldick pointed out, ismembertol() is a good approach when working with floating decimals.
James Browne 2020 年 1 月 10 日
Good point!
Also, yes, don't forget about the \n...it has been a while since I have done any MATLAB coding, I totally forgot about that!

サインインしてコメントする。

R2015a

### Community Treasure Hunt

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

Start Hunting!

Translated by