How to repeat the first number only

1 visualización (últimos 30 días)
fyza affandi
fyza affandi el 7 de Dic. de 2018
Editada: Stephan el 2 de En. de 2019
Array a is given
a =[1]
How can I add 0 to the array depending on n.
n=1 --> a=[0 1]
n=2 --> a=[0 0 1]
n=3 --> a=[0 0 0 1]
and n can be changes to any number

Respuesta aceptada

madhan ravi
madhan ravi el 7 de Dic. de 2018
Editada: madhan ravi el 7 de Dic. de 2018
a = 1;
n = 2; % just an example n
a = [zeros(1,n) a]
  2 comentarios
fyza affandi
fyza affandi el 7 de Dic. de 2018
Thank you very much :)
madhan ravi
madhan ravi el 7 de Dic. de 2018
Editada: madhan ravi el 7 de Dic. de 2018

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 7 de Dic. de 2018
madhan's way is the best (how I'd do it), but I just want to show you a neat trick - that you can "grow" an array by simply assigning something to the last location (the location at the largest point that you want to grow it to):
a = 1; % Initial array.
n = 2; % just an example n
% a = [zeros(1,n) a] % Best way
a(n+1) = 0; % Enlarge array by appending zeros simply by assigning the last element.
a = fliplr(a) % Flipr it so that the 1 is on the right.
  5 comentarios
Stephen23
Stephen23 el 7 de Dic. de 2018
Editada: Stephen23 el 7 de Dic. de 2018
Not just a neat trick, until R2015b allocating to the last element (and implicitly filling array with zeros) was faster than calling zeros:
Stephan
Stephan el 2 de En. de 2019
Editada: Stephan el 2 de En. de 2019
49996 +2

Iniciar sesión para comentar.

Categorías

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