finding all indices of a value in matrix

180 visualizaciones (últimos 30 días)
sambhav jain
sambhav jain el 18 de Feb. de 2020
Comentada: sambhav jain el 18 de Feb. de 2020
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 comentarios
Stephen23
Stephen23 el 18 de Feb. de 2020
"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.
sambhav jain
sambhav jain el 18 de Feb. de 2020
ok.thanks again

Iniciar sesión para comentar.

Respuesta aceptada

Rafael Hernandez-Walls
Rafael Hernandez-Walls el 18 de Feb. de 2020
A=[2 2 3 ;
2 4 2;
2 3 2];
[r,c]=find(A==min(A))
  2 comentarios
Stephen23
Stephen23 el 18 de Feb. de 2020
Editada: Stephen23 el 18 de Feb. de 2020
min(A)
should be
min(A(:))
otherwise this code will fail for any matrix which does not contain the minimum value in each column.
sambhav jain
sambhav jain el 18 de Feb. de 2020
thanks a lot sir

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 18 de Feb. de 2020
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.
  4 comentarios
sambhav jain
sambhav jain el 18 de Feb. de 2020
Ok
sambhav jain
sambhav jain el 18 de Feb. de 2020
I first found the minimum value in the matrix and then found the indices of the value using above as given by rafael but yours works too. Thank you

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by