Add a number every n values

11 visualizaciones (últimos 30 días)
Nikolas Spiliopoulos
Nikolas Spiliopoulos el 10 de Dic. de 2018
Respondida: Stephen23 el 10 de Dic. de 2018
Hi all,
I have a question:
If I have an array
A=[ 1 2 3 4 5 3 0 2 8 3 2 1 0 3 2 0 0 1 1 0 3 1 5 3 2 8 1 0 3 2];
Is it possible to put the a value "a=800" after every 6 elements of A; (or after every 3 elements etc.)
So I get
b=[ 1 2 3 4 5 a 3 0 2 8 3 a 2 1 0 3 2 a 0 0 1 1 0 a 3 1 5 3 2 a 8 1 0 3 2 a];
thanks a lot!!
Nikolas

Respuesta aceptada

madhan ravi
madhan ravi el 10 de Dic. de 2018
A=[ 1 2 3 4 5 3 0 2 8 3 2 1 0 3 2 0 0 1 1 0 3 1 5 3 2 8 1 0 3 2];
a=4;
after=6;
b=nan(1,numel(A)+after);
b(after:after:end)=a;
b(isnan(b))=A

Más respuestas (1)

Stephen23
Stephen23 el 10 de Dic. de 2018
>> B = reshape(A,3,[]);
>> B(4,:) = 800;
>> B = reshape(B,1,[])
B =
1 2 3 800 4 5 3 800 0 2 8 800 3 2 1 800 0 3 2 800 0 0 1 800 1 0 3 800 1 5 3 800 2 8 1 800 0 3 2 800

Categorías

Más información sobre Multidimensional Arrays 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