MATLAB: Row circular shift in matrix

MATLABrow circular shift

How can I shift all the elements of a particular row in matrix in left circular shift or right circular shift.

Best Answer

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