How can i detect max value and his index ?

45 次查看(过去 30 天)
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.

请先登录,再进行评论。

回答(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

请先登录,再进行评论。


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.

请先登录,再进行评论。


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.

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by