Shifting the first element of an array to the end

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);

 Accepted Answer

A=[ 1 2 3 4 ];
B=[A(end),A(1:end-1)]
B = 1×4
4 1 2 3

2 Comments

but how would you do it if the loop starts from the second element?

Sign in to comment.

More Answers (1)

a = 1:4;
circshift(a, 1)
ans = 1×4
4 1 2 3

1 Comment

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

Sign in to comment.

Products

Release

R2020b

Tags

Asked:

on 6 Sep 2021

Commented:

on 6 Sep 2021

Community Treasure Hunt

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

Start Hunting!