How to shift a cell in a matrix by desired number?

2 views (last 30 days)
I have a matrix which has 8x8x8 dimensions. In every cell there are values like one and zeros. here is an example of my values.
A =
1 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
like this A i have 7 more layers so it is an 8x8x8
So i want to shift every values in my matrix to the right like 7 or 15 times. In the end of the row values should pass the next coloumn and the end of the matrix values should return to the begining. values are not passing to the other layers every layer are shifting in its own. so none of the values are getting lost in a matrix just shifting. for example if i shift values to the right like 7 times, i should get this:
A1 =
0 0 0 0 0 0 1 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
i tried circshift but i didnt get anything. i hope somebody can help, thanks.

Answers (2)

KSSV
KSSV on 8 Sep 2021
Read about circshift.

Paul Kaufmann
Paul Kaufmann on 8 Sep 2021
Looking more into @KSSV's suggestion, maybe circshift will work for you, if you reshape A first:
A = [1 1 0 0; 1 1 0 0; 0 0 0 0; 0 0 0 1]
A = 4×4
1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1
[m,n] = size(A); % remember for later
B = reshape(A,1,numel(A))
B = 1×16
1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1
B2 = circshift(B,2)
B2 = 1×16
0 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0
A2 = reshape(B2,m,n)'
A2 = 4×4
0 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0
I'm not willing to look into if or how that works in a 3D-matrix case, but you can take it from here, I think.
  1 Comment
arda has
arda has on 8 Sep 2021
it doesnt matter i can apply it each layer one by one but i need it to apply on at least 8x8 matrix. in example i just put some ones to show what kind of shift i need. in normal conditions each cell have a value, i need to shift in 8x8 matrix, but if it is not possible i can reshape my matrixes like you suggested and shift them.
thanks

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!