How can I find maximum before a certain element in my matrix

3 ビュー (過去 30 日間)
Lily
Lily 2015 年 5 月 5 日
回答済み: Jos (10584) 2015 年 5 月 5 日
For example B=[5 8 5 2 6 9 10] min(B)=2
I am looking for maximum point before min(B), which is 8 (not maximum of B elements, which is 10)

採用された回答

Chad Greene
Chad Greene 2015 年 5 月 5 日
編集済み: Chad Greene 2015 年 5 月 5 日
B=[5 8 5 2 6 9 10] ;
[~,ind] = min(B);
maxB4min = max(B(1:ind))
=8
  3 件のコメント
James Tursa
James Tursa 2015 年 5 月 5 日
~ causes the output variable for that spot (1st spot in this example) to be ignored.
[~,ind] = min(B);
is essentially equivalent to
[dummy,ind] = min(B);
clear dummy
Chad Greene
Chad Greene 2015 年 5 月 5 日
編集済み: Chad Greene 2015 年 5 月 5 日
If you wanted to know the minimum value in B and its index, you'd type
[minB,ind] = min(B)
minB =
2
ind =
4
which shows that the minimum value of B is 2, which is the fourth element in B. But we don't really need to know what the minimum value in B is, so the tilde tells Matlab to not bother writing the minimum value in B to a variable and gives us only the information we need:
[~,ind] = min(B)
ind =
4

サインインしてコメントする。

その他の回答 (2 件)

Star Strider
Star Strider 2015 年 5 月 5 日
This works:
B = [5 8 5 2 6 9 10];
[~,ix] = min(B);
MaxBeforeMin = max(B(1:ix))
producing:
MaxBeforeMin =
8
The code searches for the index of the first value of the minium and returns it as ‘ix’. It then takes the maximum up to that index, and reports it as ‘MaxBeforeMin’.

Jos (10584)
Jos (10584) 2015 年 5 月 5 日
Just for fun, as a one-liner:
B = [5 8 5 2 6 9 10];
MaxBeforeMin = max(B(1:find(B==min(B),1,'first')))

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by