Manipulating an array with an octave circcift / matlab

1) I am trying to move an array (outer cells) to the first and last cell inward at the same time. Here's an internal animation of what I am trying to do with an array internal animation . As you can see, the outer cells move inward (from the ends) at the same time

Here's a picture, but the animation shows it much better, note that the array can have an even or odd number of cells

Inner rotation steps
1 2 3 4 5 6 7
4 1 2 3 6 7 5
3 4 1 2 7 5 6

      

Inward direction

2) I am trying to move the middle cells of an array outward using the circshift method (which I think is the fastest) at the same time. Here's an external animation of what I am trying to do external animation . As you can see, the middle of the waveform moves outward (left and right) at the same time.

Here's a picture, but the animation shows it much better, note that the array can have an even or odd number of cells

Outer rotation steps
1 2 3 4 5 6 7
2 3 4 1 7 5 6
3 4 1 2 6 5 7

      

enter image description here

Example: inward
a = (1:7)
y=circshift(A,[0 -2]) %shift end of array inward
3   4   5   6   7   1   2

a = (1:7)
y=circshift(A,[0 2]) %shift beginning of array inward
6   7   1   2   3   4   5

      

Not sure how to make middle cells move outward using circular movement or outer cells moving inward at the same time

I'm not sure how to start a circular motion from the center and move the array outward / inward to get this effect.

Please note that I am not trying to get this equation. I am just trying to get the arrays to move in the same way. I am using octave 3.8.1 which is Matlab compatible.

+3


source to share


2 answers


A = 1:7;

split = ceil(numel(A)/2);

n = 2;
A(1:split) = circshift(A(1:split), [0, n]);
A(split+1:end) = circshift(A(split+1:end), [0, -n]);

      



Put the last three lines in a loop if you like. Also just change the signs n

for inward or outward

+2


source


how to build new indexes instead of using circshift:

A = 1:7;

halfLen = ceil(length(A)/2); % or use ceil to 
idcsOutward = [2:halfLen,1,length(A),(halfLen+1):(length(A)-1)];

B1 = A(idcsOutward)
B2 = B1(idcsOutward)

% and inward:
idcsInward = [halfLen,1:(halfLen-1),(halfLen+2):length(A),halfLen+1];

C1 = A(idcsInward)
C2 = C1(idcsInward)

      



Result:

B1 =
     2     3     4     1     7     5     6

B2 =
     3     4     1     2     6     7     5

C1 =
     4     1     2     3     6     7     5

C2 =
     3     4     1     2     7     5     6

      

0


source







All Articles