finding all indices of a value in matrix
237 次查看(过去 30 天)
显示 更早的评论
i have a matrix say
[2 2 3 ;
2 4 2;
2 3 2]
how do i find all the indices (row, column) of 2 (minimum value element of matrix) in the given matrix?
4 个评论
Stephen23
2020-2-18
"but could you plz tell how is it better by not using find?"
It isn't better, it just demonstrates that every problem can be approached in different ways, and hopefully shows a different way to think about the problem.
采纳的回答
Rafael Hernandez-Walls
2020-2-18
A=[2 2 3 ;
2 4 2;
2 3 2];
[r,c]=find(A==min(A))
更多回答(1 个)
Steven Lord
2020-2-18
Do you need the locations or do you simply need to refer to the locations where the specified value occurs? Those are two slightly different things. If the latter, you don't need to use find and don't need to compute the indices at all.
Use logical indexing.
A = [2 2 3 ; 2 4 2; 2 3 2]
B = A; % Make a copy of A for future reference
two = A == 2 % Create a logical mask
B(two) = -1 % Replace all 2's in (the copy of) A with -1
In this example, I don't care where the 2's were in A. I only cared that I could change those elements.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!