How can i detect max value and his index ?

45 ビュー (過去 30 日間)
benghenia aek
benghenia aek 2019 年 1 月 22 日
コメント済み: benghenia aek 2019 年 1 月 23 日
I have vector a=[1 2 5 NAN NAN 9 0 23 12 NAN NAN NAN 6 2 8]
how to detect max value and it index ??
a(1)=[5] and index index(1)=[3]
a(2)=[23] and index index(2)=[8]
a(3)=[8] and index index(n)=[15]
  2 件のコメント
Rik
Rik 2019 年 1 月 22 日
Please use meaningfull tags. Using the names of contributors will not magically attract them to your question. Also, it would have been better to provide a link to your previous question for a bit more context.
benghenia aek
benghenia aek 2019 年 1 月 23 日
thank you very much for your assistance

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

回答 (4 件)

Rik
Rik 2019 年 1 月 22 日
編集済み: Rik 2019 年 1 月 22 日
Loop through the elements of your cell vector a, use the max function to find the maximum value for a, and the second output of max to find which index in index you need to keep.
Using the strategy by Jan (with a minor edit):
a = [1 2 5 NaN NaN 9 0 23 12 NaN NaN NaN 6 2 8];
m = [true, isnan(a), true];
ini = strfind(m, [true, false])-1;
fin = strfind(m, [false, true])-1;
n = numel(ini);
Result = cell(1, n);
Index = cell(1, n);
Result_max = cell(1, n);
Index_max = cell(1, n);
for k = 1:n
Index{k} = ini(k)+1:fin(k);
Result{k} = a(ini(k)+1:fin(k));
[Result_max{k},idx]=max(Result{k});
Index_max{k}=Index{k}(idx);
end
celldisp(Result)
celldisp(Index)
celldisp(Result_max)
celldisp(Index_max)

TADA
TADA 2019 年 1 月 22 日
編集済み: TADA 2019 年 1 月 22 日
If you meant to split the array into subarrays according to locations of NaN, then find maximal values of each subarray but the index of each maximal value in the original array:
a=[1 2 5 nan nan 9 0 23 12 nan nan nan 6 2 8];
numIdx = find(~isnan(a));
values = a(numIdx);
startAt = numIdx([true diff(numIdx) > 1]);
ii = find(diff(numIdx) > 1);
splittingMask = [ii(1) diff(ii) numel(values)-ii(end)];
c = mat2cell(values, 1, splittingMask);
maxVal = [zeros(1, numel(c));startAt-1];
for i = 1:numel(c)
[mv, mi] = max(c{i});
maxVal(:,i) = maxVal(:,i) + [mv; mi];
end
maxVal
maxVal =
5 23 8
3 8 15
  2 件のコメント
TADA
TADA 2019 年 1 月 22 日
or alternatively:
maxVal1 = {};
currMax = [nan;0];
for i = 1:numel(a)
if isnan(a(i))
if ~isnan(currMax(1))
maxVal1{numel(maxVal1)+1} = currMax;
currMax = [nan;0];
end
continue;
end
if isnan(currMax(1)) || a(i) > currMax(1)
currMax = [a(i);i];
end
end
if ~isnan(currMax(1))
maxVal1{numel(maxVal1)+1} = currMax;
currMax = [nan;0];
end
maxVal2 = cell2mat(maxVal1);
maxVal2
maxVal2 =
5 23 8
3 8 15
benghenia aek
benghenia aek 2019 年 1 月 23 日
thank you very much for your assistance

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


Kevin Phung
Kevin Phung 2019 年 1 月 22 日
編集済み: Kevin Phung 2019 年 1 月 22 日
[max_val idx] = max(a)
max_val would be the max value in your array a,
idx would be the index.
i have no idea what
'a(1)=[5] and index index(1)=[3]
a(2)=[23] and index index(2)=[8]
a(3)=[8] and index index(n)=[15]'
means though.
  2 件のコメント
Rik
Rik 2019 年 1 月 22 日
The odd notation is the same as in his other question, where we implicitly assumed he wanted a cell array.
benghenia aek
benghenia aek 2019 年 1 月 23 日
thank you very much for your assistance

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


Brian Hart
Brian Hart 2019 年 1 月 22 日
Let's say your vector is
a =[1 2 5 NaN NaN 9 0 23 12 NaN NaN NaN 6 2 8]
Then the max value is given by:
>> max(a)
ans =
23
and the index of that value is given by:
>> find(a==max(a))
ans =
8
I'm not sure what you mean by a(1)=[5] (it would be "1"), a(2)=[23] (really "2"), etc.
  1 件のコメント
benghenia aek
benghenia aek 2019 年 1 月 23 日
thank you very much for your assistance

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

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by