Swap between two sub array of an array
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Amine Ne
el 20 de Ag. de 2018
Comentada: Amine Ne
el 22 de Ag. de 2018
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 comentarios
Respuesta aceptada
Stephan
el 20 de Ag. de 2018
Editada: Stephan
el 20 de Ag. de 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 comentarios
Stephan
el 20 de Ag. de 2018
Editada: Stephan
el 20 de Ag. de 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.
Más respuestas (1)
Amine Ne
el 20 de Ag. de 2018
5 comentarios
Stephan
el 21 de Ag. de 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
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!