Two Same max Values of a Array

조회 수: 17 (최근 30일)
Eren Atar
Eren Atar 2021년 6월 16일
댓글: Eren Atar 2021년 6월 17일
I want to find and increase the Value of an Array by 1.
But I also want to look into cases with multiple maximas with the same value.
A = [22 22 17 18 12];
[val,idx]= max(A);
A(idx)= A(idx)+1;
By default it increases the first element of the array. In my case it should select the second one or should increase it randomly if there are multiple maximas.
Any help would be appreciated.

채택된 답변

Jonas
Jonas 2021년 6월 16일
get the maximum value and search for entries equal to that value
maxEntriesIdx=find(A==max(A));
if you want to increase one random maximum value you can use e.g.
whichEntry=randi(numel(maxEntriesIdx));
A(maxEntriesIdx(whichEntry))=A(maxEntriesIdx(whichEntry))+1;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by