Borrar filtros
Borrar filtros

Shift array by one element backwards or forward

50 visualizaciones (últimos 30 días)
John Jarvis
John Jarvis el 17 de Feb. de 2020
Editada: Captain Karnage el 16 de Mayo de 2023
For example, I have an array indexed by n, running from -n to +n
A_n=[-n,..-1,0,1,.....]
I want to create a new array A_{n+1} which will shift every element of A_n one place forward
and A_{n-1} which will shift every element of A_n one place backwards.
How to do it?

Respuesta aceptada

Bhaskar R
Bhaskar R el 17 de Feb. de 2020
x = -5:5; % array
fr = circshift(x, 1); % forward
bk = circshift(x, -1); % backward
  3 comentarios
Bhaskar R
Bhaskar R el 17 de Feb. de 2020
"Warning: CIRCSHIFT(X,K) with scalar K and where size(X,1)==1 will change behavior in future versions. To
retain current behavior, use CIRCSHIFT(X,[K,0]) instead. "
As warning suggested modify command as
x = -5:5; % array
fr = circshift(x, [0, 1]); % forward
bk = circshift(x, [0, -1]); % backward
Captain Karnage
Captain Karnage el 16 de Mayo de 2023
Editada: Captain Karnage el 16 de Mayo de 2023
So this depends on whether you have a horizontal or vertical vector. @Bhaskar R's example works for a horizontal vector (-5:5 will give you a horizontal vector). For a vertical vector it will be just like the suggestion in the warning:
x = -5:5; % array
fr = circshift(x, [1, 0]); % forward
bk = circshift(x, [-1, 0]); % backward
I can confirm that is what worked for me.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos


Versión

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by