Selecting two values of an array randomly and reversing the values between
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Mathieu NOE
on 19 Jan 2023
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)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!