Filter löschen
Filter löschen

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

12 Ansichten (letzte 30 Tage)
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);

Akzeptierte Antwort

Image Analyst
Image Analyst am 15 Sep. 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 Kommentar
Andrew Padilla
Andrew Padilla am 15 Sep. 2018
Bearbeitet: Andrew Padilla am 15 Sep. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by