How to rewrite the maximum element of an array by an avarage

조회 수: 1 (최근 30일)
Serhii Sheinych
Serhii Sheinych 2019년 12월 2일
댓글: Serhii Sheinych 2019년 12월 2일
I have an array of 100 random numbers and i need to detect the maximum value of an array and then replace it by mean value of an array.
I have no idea how to do it, help please.

채택된 답변

ME
ME 2019년 12월 2일
So, if I have interpreted your question correctly then you have an array (say A) and you wish to find the maximum valued element in A. Once found you then want to replace that maximum element with the mean of the elements in A? If that is right then you can do that with:
A(find(A==max(A)))=mean(A);
If that's not what you wanted then you may need to provide some more details on what exactly you want this to do.
  댓글 수: 3
ME
ME 2019년 12월 2일
So you could definitely find the mean without the maximum value using:
B = sort(A);
C = mean(B(1:end-1));
where C is the mean of the array excluding the maximum element. Then if you wanted to replace the maximum element then you'd obviously have to use:
A(find(A==max(A))) = C;
Of course if the order of your random elements doesn't matter to you then you could sort the elements first and then save yourself from having to make a sorted duplicate of your A array (not a problem for small examples but if your arrays are going to get very large then this may become an issue.
A = sort(A);
A(find(A==max(A))) = mean(A(1:end-1));
Serhii Sheinych
Serhii Sheinych 2019년 12월 2일
Understood. Thank You so much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by