Borrar filtros
Borrar filtros

How can I print the positions that a value occur in a row array?

12 visualizaciones (últimos 30 días)
If I have the matrix
A = [0, 2, 2, 6, 4, 3, 10]
and
Minimum = min(A)
How do I print the column positions that each minimum value occurs at? The statement below works only if there is only one minimum value present.
Assume X is the value of the position of the minimum value.
How can I print the positions if the minimum value occurs more than once?
fprintf('\nMinimum Temperature [degrees C]: %f (at position(s): %f)',Minimum, X);

Respuesta aceptada

Image Analyst
Image Analyst el 15 de Sept. de 2018
Try this:
X = randi(9, 1, 40)
minTemperature = min(X);
indexes = find(X == min(X));
fprintf('Minimum Temperature of %.3f degrees C:\n', minTemperature);
fprintf(' at position: %d\n', indexes);
Sample results:
X =
Columns 1 through 21
2 7 5 5 8 8 5 3 2 8 9 9 6 1 1 2 1 7 2 6 8
Columns 22 through 40
6 4 8 3 3 8 5 3 7 2 9 3 1 1 7 8 7 7 9
Minimum Temperature of 1.000 degrees C:
at position: 14
at position: 15
at position: 17
at position: 34
at position: 35
  1 comentario
Andrew Padilla
Andrew Padilla el 15 de Sept. de 2018
Editada: Andrew Padilla el 15 de Sept. de 2018
I receive the following errors when running the code:
Index exceeds array bounds.
Array indices must be positive integers or logical values.
UPDATE:
I have fixed the issue. I kept the code that I was originally using except I separated the print statements into two like you did in the code you provided above. Thank you.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures 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