Selecting two values of an array randomly and reversing the values between

2 ビュー (過去 30 日間)
John
John 2023 年 1 月 19 日
コメント済み: Mathieu NOE 2023 年 1 月 31 日
Hi everyone. How can I randomly select two values in an array, and then reverse all the values between them? (Including the values selected)

回答 (1 件)

Mathieu NOE
Mathieu NOE 2023 年 1 月 19 日
hello
try this
the output has values between 2nd and 5th data flipped
n = 10;
A = (1:n) % array
A = 1×10
1 2 3 4 5 6 7 8 9 10
ind1 = 2; % first index
ind2 = 5; % second index
B = A;
B(ind1:ind2) = B(ind2:-1:ind1)
B = 1×10
1 5 4 3 2 6 7 8 9 10

カテゴリ

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

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by