How do I switch location of the largest and smallest elements of an array?
1 次查看(过去 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!