To get the column number of max and min number

조회 수: 19 (최근 30일)
fyza affandi
fyza affandi 2019년 2월 17일
댓글: fyza affandi 2019년 2월 17일
I have an array,
a=[ 1 5 3 7 4]
How can I get the position of max and min numbere?
The answer should be
max = 4 %(number 7 which is at column 4 )
min = 1 %(number 1 which is at column 1 )

채택된 답변

Stephen23
Stephen23 2019년 2월 17일
>> a = [1,5,3,7,4]
a =
1 5 3 7 4
>> [~,mxi] = max(a)
mxi = 4
>> [~,mni] = min(a)
mni = 1

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 2월 17일
You can use the min() and max() functions but they have a serious limitation with integer numbers - they only return the FIRST occurrence of the max or min. If it occurs more than once you will not get all the indexes. Try this demo code and understand the results:
a=[ 1 5 3 7 4, 7, 1]
[maxValue, indexOfMax] = max(a)
[minValue, indexOfMin] = min(a)
indexesOfMaxs = find(a == maxValue)
indexesOfMins = find(a == minValue)
The results in the command window:
a =
1 5 3 7 4 7 1
maxValue =
7
indexOfMax =
4
minValue =
1
indexOfMin =
1
indexesOfMaxs =
4 6
indexesOfMins =
1 7
Note how using find() gives you both indexes rather than just the first one.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by