How to add the value of the 3 highest elements automatically from a vector?
조회 수: 1 (최근 30일)
이전 댓글 표시
I want to add the 3 highest elements of 'y' to 'sk'. But I don't know what code to use to automatically add them. in this case, the elements in question are 12, 12, and 13. So that the value will be 12 + 9 = 21, 12 + 9 = 21, 13 + 9 = 22. This is my code
clc;
clear all;
x1=[1 2 3 4 5 6 7 8 9 10 11];
x2=[1 2 3 4 5 6 7 8 9 10 11];
y=[10 9 11 10 11 11 9 10 12 12 13];
sx=9;
and the result will be
y = [10 9 11 10 11 11 9 10 21 21 22]
댓글 수: 0
답변 (3개)
Peng Li
2020년 3월 28일
[~, ind] = sort(y);
y(ind(end-2, end)) = y(ind(ind-2, end)) + ax;
Should meet your need.
댓글 수: 4
J. Alex Lee
2020년 3월 28일
like this?
[~,idx] = maxk(y,3)
y(idx) = y(idx) + sx
Are x1 and x2 irrelevant to your question?
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!