How do I switch location of the largest and smallest elements of an array?
조회 수: 3 (최근 30일)
이전 댓글 표시
vect = randi(20,1,15)
partA = round(vect)
partB=min(partA)
partC=max(partA)
MeanValue=mean(vect)
ind = find(partA~=MeanValue)
partD=ind
partE=sort(partA,'descend')
Now how do I switch largest and smallest element of partE?
댓글 수: 0
답변 (1개)
Chunru
2021년 9월 17일
vect = randi(20,1,15)
[~, imax] = max(vect)
[~, imin] = min(vect)
% swap
tmp = vect(imax);
vect(imax) = vect(imin);
vect(imin) = tmp;
vect
댓글 수: 2
Chunru
2021년 9월 17일
Do you mean this?
vect_new = vect; % copy the vector to a new one
tmp = vect_new(imax);
vect_new(imax) = vect_new(imin);
vect_new(imin) = tmp;
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!