필터 지우기
필터 지우기

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

조회 수: 12 (최근 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에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by