How to shift a cell in a matrix by desired number?
8 次查看(过去 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.
0 个评论
回答(2 个)
Paul Kaufmann
2021-9-8
A = [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))
B2 = circshift(B,2)
A2 = reshape(B2,m,n)'
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!