필터 지우기
필터 지우기

Shifting the first element of an array to the end

조회 수: 32 (최근 30일)
Dillon La
Dillon La 2021년 9월 6일
댓글: Dillon La 2021년 9월 6일
Hi all, I.m trying to transform vector A into B as below,
A B
[ 1 2 3 4 ] [ 4 1 2 3 ]
Here is what I have got so far, but it didnt work out, please point out where I got it wrong
A = [1 2 3 4];
B = [A(1)];
for i = [2:length(A)]
B = [A(i) B]
end
disp(B);

채택된 답변

Matt J
Matt J 2021년 9월 6일
A=[ 1 2 3 4 ];
B=[A(end),A(1:end-1)]
B = 1×4
4 1 2 3
  댓글 수: 2
Dillon La
Dillon La 2021년 9월 6일
thanks a bunch!!!
Dillon La
Dillon La 2021년 9월 6일
but how would you do it if the loop starts from the second element?

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

추가 답변 (1개)

Ive J
Ive J 2021년 9월 6일
a = 1:4;
circshift(a, 1)
ans = 1×4
4 1 2 3
  댓글 수: 1
Dillon La
Dillon La 2021년 9월 6일
thank you, but do you have a simpler version of it?
I'm trying to figure out what I got wrong rather than using a new operator

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by