Dear all,
I have a variable that I named it 'Selec' that was inside a for-loop and it become like this
Selec = [0] [0] [1] [0] [0] [0]
I'm trying to know the location of the highest value. In this example it is 3. So how can I do that?
I tried to use the following 'max' but it is not working
[value,index] = max(Selec);
So is it possible to make Select like this
Selec = [001000] such that I can use 'max' to find the location of 1 which is 3 in this example.
Any help will be appreciated.
Meshoo

 採用された回答

Jos (10584)
Jos (10584) 2014 年 1 月 15 日

0 投票

Apparently, Selec is a cell array. You can convert into a numerical array by concatenation:
Selec = {[0] [0] [1] [0] [0] [0] }
Selec2 = [Selec{:}]
[value,index] = max(Selec2)

4 件のコメント

Meshooo
Meshooo 2014 年 1 月 15 日
That's correct. Thank you very much.
Meshooo
Meshooo 2014 年 1 月 15 日
I still have a small problem. Sometimes I will have the following value for Select
Selec = {[0] [0] [0] [0] [0] [0] }
There is no max value because all are zeros. In such a case, if I applied the max then index value = 1 which is not correct.
Selec2 = [Selec{:}]
[value,index] = max(Selec2)
>>index = 1
Do you have idea how to solve that?
Jos (10584)
Jos (10584) 2014 年 1 月 15 日
Stricty speaking, zero is the maximum value. If you want to exclude specific maximum values, you could adopt something like:
[value,index] = max(Selec2)
if value == 0,
index = []
value = NaN
end
Meshooo
Meshooo 2014 年 1 月 16 日
You are right. Thank you very much..

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

その他の回答 (0 件)

カテゴリ

質問済み:

2014 年 1 月 15 日

編集済み:

2014 年 1 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by