Shifting the first element of an array to the end

46 visualizaciones (últimos 30 días)
Dillon La
Dillon La el 6 de Sept. de 2021
Comentada: Dillon La el 6 de Sept. de 2021
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);

Respuesta aceptada

Matt J
Matt J el 6 de Sept. de 2021
A=[ 1 2 3 4 ];
B=[A(end),A(1:end-1)]
B = 1×4
4 1 2 3
  2 comentarios
Dillon La
Dillon La el 6 de Sept. de 2021
thanks a bunch!!!
Dillon La
Dillon La el 6 de Sept. de 2021
but how would you do it if the loop starts from the second element?

Iniciar sesión para comentar.

Más respuestas (1)

Ive J
Ive J el 6 de Sept. de 2021
a = 1:4;
circshift(a, 1)
ans = 1×4
4 1 2 3
  1 comentario
Dillon La
Dillon La el 6 de Sept. de 2021
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

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by