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

3 次查看(过去 30 天)
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.

回答(2 个)

KSSV
KSSV 2021-9-8
Read about circshift.

Paul Kaufmann
Paul Kaufmann 2021-9-8
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 个评论
arda has
arda has 2021-9-8
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by