Shift array by one element backwards or forward

조회 수: 37(최근 30일)
John Jarvis
John Jarvis 2020년 2월 17일
댓글: Bhaskar R 2020년 2월 17일
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?

채택된 답변

Bhaskar R
Bhaskar R 2020년 2월 17일
x = -5:5; % array
fr = circshift(x, 1); % forward
bk = circshift(x, -1); % backward
  댓글 수: 2
Bhaskar R
Bhaskar R 2020년 2월 17일
"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

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by