Selecting two values of an array randomly and reversing the values between
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone. How can I randomly select two values in an array, and then reverse all the values between them? (Including the values selected)
0 个评论
回答(1 个)
Mathieu NOE
2023-1-19
hello
try this
the output has values between 2nd and 5th data flipped
n = 10;
A = (1:n) % array
ind1 = 2; % first index
ind2 = 5; % second index
B = A;
B(ind1:ind2) = B(ind2:-1:ind1)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!