Swap between two sub array of an array

2 views (last 30 days)
Swap between two sub sequences. Position one and two are selected randomly first and then position three and four. Provided these selections do not overlap then element sequence between position one and two and element sequence between position three and four are swapped.
Example: idx1 = [2 4], idx2 = [7 10], A = [2 5 3 4 7 1 6 9 8 10 11 12] would become Anew=[2 6 9 8 10 7 1 5 3 4 11 12]
Thank you
  6 Comments
Stephan
Stephan on 20 Aug 2018
Edited: Stephan on 20 Aug 2018
do you want idx1 to be another length then idx2 - im asking because idx1 has length = 3 and idx2 is length = 4
Amine Ne
Amine Ne on 20 Aug 2018
Edited: Amine Ne on 20 Aug 2018
Sorry to be late, Yes Stephan, I want idx1 to be another length then idx2, but not always. So we have two possibilties: length of idx1 = length of idx2 OR length of idx1 =! length of idx2
Thank you Stephan

Sign in to comment.

Accepted Answer

Stephan
Stephan on 20 Aug 2018
Edited: Stephan on 20 Aug 2018
If i assume that you want to swap idx which have the same length:
A = [2 5 3 4 7 1 6 9 8 10 11 12]
idx1 = [2 4];
idx2 = [7 9];
A_new = A;
A_new(idx1(1):idx1(2)) = A(idx2(1):idx2(2));
A_new(idx2(1):idx2(2)) = A(idx1(1):idx1(2))
gives:
A =
2 5 3 4 7 1 6 9 8 10 11 12
A_new =
2 6 9 8 7 1 5 3 4 10 11 12
EDIT:
This works for your example:
A = [2 5 3 4 7 1 6 9 8 10 11 12]
idx1 = [2 4];
idx2 = [7 10];
A_new = A(1:idx1(1)-1);
A_new = [A_new, A(idx2(1):idx2(2))];
A_new = [A_new, A(idx1(2)+1:idx2(1)-1)];
A_new = [A_new, A(idx1(1):idx1(2))];
A_new = [A_new, A(idx2(2)+1:end)]
result:
A =
2 5 3 4 7 1 6 9 8 10 11 12
A_new =
2 6 9 8 10 7 1 5 3 4 11 12
I did not test other examples.
Best regards
Stephan
  3 Comments
Stephan
Stephan on 20 Aug 2018
Edited: Stephan on 20 Aug 2018
In this example idx1 intersects with idx2.
If:
  • idx1(2) < idx2(1) and
  • idx1 and idx2 are sorted ascending
then they do not overlap eachother and it should work.
idx1 = [4 5];
idx2 = [9 12];
Did work when i tested. Your example (which not meets the conditions above) did not work. So when creating the random numbers it is needed to make sure meeting this conditions.
Amine Ne
Amine Ne on 20 Aug 2018
Edited: Amine Ne on 20 Aug 2018
Thank you Stephan Jung, The solution is:
nVar = 12;
A = randperm(nVar);
idx1 = randi([1,nVar-2], 1, 2);
idx1 = sort(idx1);
idx2 = randi([idx1(2)+1,nVar], 1, 2);
idx2 = sort(idx2);
A_new = A(1:idx1(1)-1);
A_new = [A_new, A(idx2(1):idx2(2))];
A_new = [A_new, A(idx1(2)+1:idx2(1)-1)];
A_new = [A_new, A(idx1(1):idx1(2))];
A_new = [A_new, A(idx2(2)+1:end)];
Result:
A =
1 6 4 8 10 11 3 12 2 5 7 9
idx1 =
5 6
idx2 =
9 12
A_new =
1 6 4 8 2 5 7 9 3 12 10 11

Sign in to comment.

More Answers (1)

Amine Ne
Amine Ne on 20 Aug 2018
Thank you Stephan Jung, The solution is:
nVar = 12;
A = randperm(nVar);
idx1 = randi([1,nVar-2], 1, 2);
idx1 = sort(idx1);
idx2 = randi([idx1(2)+1,nVar], 1, 2);
idx2 = sort(idx2);
A_new = A(1:idx1(1)-1);
A_new = [A_new, A(idx2(1):idx2(2))];
A_new = [A_new, A(idx1(2)+1:idx2(1)-1)];
A_new = [A_new, A(idx1(1):idx1(2))];
A_new = [A_new, A(idx2(2)+1:end)];
Result:
A =
1 6 4 8 10 11 3 12 2 5 7 9
idx1 =
5 6
idx2 =
9 12
A_new =
1 6 4 8 2 5 7 9 3 12 10 11
  5 Comments
Stephan
Stephan on 21 Aug 2018
Do you have to write your own algorithm? If not i would suggest to use Global Optimization Toolbox (if you have access to it), since there are possibilities of parallelisation (if you have access to Parallel omputing Toolbox) and the code is optimized by professionals.
If you have or want to write your own code i would suggest to do the following:
I think it would be worthwhile to search the forum and the documentation for vectorization, parallelization and performance enhancement and related topics.
There are numerous contributions (accepted answers) specifically from the MVP contributers (e.g. Walter Roberson, Jan, Stephen Cobeldick, John D'Errico, Star Strider, Guillaume, dpb, KSSV ... and many others) in which people have asked for options on their code to optimize and speed up.
Especially James Tursa (according to his profile) is interested in speed and performance and has certainly given some good hints in his answers.
These are certainly good sources to get suggestions for your project.
Another way (you can do by yourself) is to check and improve your code by using the Profiler feature and the Analyze Code Button. These tools show you where in your code how much time is spent or where you have typical errors. This may provide evidence of improvement.
I suggest you to try these ways and in places where you have questions to come back with a new question here. It's always amazing to see how well some people can deal with Matlab and how much knowledge is available here.
As an example read this link where i asked for the possibilities of improvement from a matlab code. The answer John D'Errico gave me brought me a lot of insight to this topic.
Thats what i can suggest you.
Best regards
Stephan
Amine Ne
Amine Ne on 22 Aug 2018
Hi Stephan,
Thank you very much for your detailed and helpful explanation. I have already implemented an algorithm with my own code. I work on bio-inspired algorithms such as ABC, BA, PSO...
I will take your suggestions into consideration. Thank you again

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!