row circular shift in matrix
26 次查看(过去 30 天)
显示 更早的评论
How can I shift all the elements of a particular row in matrix in left circular shift or right circular shift.
0 个评论
采纳的回答
Tommy
2020-4-20
编辑:Tommy
2020-4-23
M(row,:) = [M(row,end) M(row,1:end-1)]; % shift to the right
M(row,:) = [M(row,2:end) M(row,1)]; % shift to the left
(edit) To shift by any amount:
M = randi(10,5)
shift = 8; row = 2;
[n,m] = size(M);
M(row,:) = [M(row,(end-mod(shift,m)+1):end) M(row,1:(end-mod(shift,m)))] % shift to the right
M(row,:) = [M(row,(mod(shift,m)+1):end) M(row,1:mod(shift,m))] % shift to the left
(edit) Fixed mistake
3 个评论
Tommy
2020-4-23
Yes sorry I goofed! I used n where I should've used m. Let me know if that fixes it for you.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!