Array operation shift values

1 visualización (últimos 30 días)
Ali Mukhtar
Ali Mukhtar el 18 de Mzo. de 2021
Comentada: Walter Roberson el 18 de Mzo. de 2021
i have an array of A=[0 0 0 0 0 ] im sending first value "3" array become A=[3 0 0 0 0] ... then when i send next value for e.g. "5" then array should be A=[5 3 0 0 0] and then if "1" A=[ 1 5 3 0 0] . i cant identify command for this purpose

Respuesta aceptada

Matt J
Matt J el 18 de Mzo. de 2021
Editada: Matt J el 18 de Mzo. de 2021
values=[3,5,1];
A=[0 0 0 0 0 ];
for i=1:numel(values)
A=[values(i), A(1:end-1)]
end
A = 1×5
3 0 0 0 0
A = 1×5
5 3 0 0 0
A = 1×5
1 5 3 0 0
  2 comentarios
Ali Mukhtar
Ali Mukhtar el 18 de Mzo. de 2021
the values coming in array are serially entered and they are random value... not fix
Matt J
Matt J el 18 de Mzo. de 2021
It shouldn't matter.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 18 de Mzo. de 2021
A = zeros(1,5);
for K = [3 5 1]
A = [K, A(1:end-1)]
end
A = 1×5
3 0 0 0 0
A = 1×5
5 3 0 0 0
A = 1×5
1 5 3 0 0
  2 comentarios
Ali Mukhtar
Ali Mukhtar el 18 de Mzo. de 2021
the values coming in array are serially entered and they are random value... not fix
Walter Roberson
Walter Roberson el 18 de Mzo. de 2021
A = zeros(1,5);
for K = 1:5
A = [randi(9), A(1:end-1)]
end

Iniciar sesión para comentar.

Categorías

Más información sobre Operators and Elementary Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by