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

2 次查看(过去 30 天)
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);

采纳的回答

Image Analyst
Image Analyst 2018-9-15
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 个评论
Andrew Padilla
Andrew Padilla 2018-9-15
编辑:Andrew Padilla 2018-9-15
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by