Borrar filtros
Borrar filtros

How can i detect max value and his index ?

16 visualizaciones (últimos 30 días)
benghenia aek
benghenia aek el 22 de En. de 2019
Comentada: benghenia aek el 23 de En. de 2019
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 comentarios
Rik
Rik el 22 de En. de 2019
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 el 23 de En. de 2019
thank you very much for your assistance

Iniciar sesión para comentar.

Respuestas (4)

Rik
Rik el 22 de En. de 2019
Editada: Rik el 22 de En. de 2019
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 el 22 de En. de 2019
Editada: TADA el 22 de En. de 2019
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 comentarios
TADA
TADA el 22 de En. de 2019
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 el 23 de En. de 2019
thank you very much for your assistance

Iniciar sesión para comentar.


Kevin Phung
Kevin Phung el 22 de En. de 2019
Editada: Kevin Phung el 22 de En. de 2019
[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 comentarios
Rik
Rik el 22 de En. de 2019
The odd notation is the same as in his other question, where we implicitly assumed he wanted a cell array.
benghenia aek
benghenia aek el 23 de En. de 2019
thank you very much for your assistance

Iniciar sesión para comentar.


Brian Hart
Brian Hart el 22 de En. de 2019
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.

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by