How do I move an item in an array to the end of that array?

2 ビュー (過去 30 日間)
Steve Franks
Steve Franks 2021 年 11 月 28 日
回答済み: Chunru 2021 年 11 月 28 日
I'm having trouble getting an array to display right, because one of the numbers keeps showing up in the wrong place. Is there any way to move it from one part of the array to the end, without swapping it with another variable?
Here's an example of what I have (with T being an array):
T = 1 5 77 10 15 20 25 33
Here's an example of what I need:
T = 1 5 10 15 20 25 33 77
Thank you kindly in advance!

回答 (2 件)

Image Analyst
Image Analyst 2021 年 11 月 28 日
indexToMove = 3;
T = [1 5 77 10 15 20 25 33];
T = [T(1:indexToMove-1), T(indexToMove+1:end), T(indexToMove)]
T = 1×8
1 5 10 15 20 25 33 77

Chunru
Chunru 2021 年 11 月 28 日
T = [1 5 77 10 15 20 25 33];
T1 = sort(T) % Method 1
T1 = 1×8
1 5 10 15 20 25 33 77
T2 = T([1:2 4:end 3]) % Method2
T2 = 1×8
1 5 10 15 20 25 33 77

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by