How do I switch location of the largest and smallest elements of an array?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Chunru
el 17 de Sept. de 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 comentarios
Chunru
el 17 de Sept. de 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;
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!