How do I switch location of the largest and smallest elements of an array?
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Chunru
on 17 Sep 2021
vect = randi(20,1,15)
[~, imax] = max(vect)
[~, imin] = min(vect)
% swap
tmp = vect(imax);
vect(imax) = vect(imin);
vect(imin) = tmp;
vect
2 Comments
Chunru
on 17 Sep 2021
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;
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!