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!