MATLAB: How to shift specific elements in a array to a specific position

shift array

I have an arry A = [1,2,3,4,5,6,7,8,9,10]. I want specific elements to shift the order but remain with the same elements. E.x. I want 3 elements (7,8,9) in A to shift 3 postions to the left to get an array B = [1,2,3,7,8,9,4,5,6,10]
Thanks,
Nur

Best Answer

Hello Nur!
Just shift the positions in the vector if you know the current position,
A = [1,2,3,4,5,6,7,8,9,10]
startPos = 7;
numElements = 3;
B = A;
oldElements = A(startPos-numElements:startPos-1);
B(startPos-numElements:startPos-1)=A(startPos:startPos+numElements-1);
B(startPos:startPos+numElements-1)=oldElements