row circular shift in matrix

26 visualizaciones (últimos 30 días)
Abhishek Bakhla
Abhishek Bakhla el 20 de Abr. de 2020
Comentada: Abhishek Bakhla el 24 de Abr. de 2020
How can I shift all the elements of a particular row in matrix in left circular shift or right circular shift.

Respuesta aceptada

Tommy
Tommy el 20 de Abr. de 2020
Editada: Tommy el 23 de Abr. de 2020
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 comentarios
Abhishek Bakhla
Abhishek Bakhla el 23 de Abr. de 2020
Hi, it seems to me that shift to left by any amount is not working could you please verify ?
Tommy
Tommy el 23 de Abr. de 2020
Yes sorry I goofed! I used n where I should've used m. Let me know if that fixes it for you.

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 23 de Abr. de 2020
Simpler using circshift, where k<0 shifts to the left and k>0 shifts to the right:
M(row,:) = circshift(M(row,:),k,2)

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by