Borrar filtros
Borrar filtros

repeating elements of a vector in a certain manner

1 visualización (últimos 30 días)
Mnr
Mnr el 23 de Feb. de 2016
Respondida: Star Strider el 23 de Feb. de 2016
Hello all, I have a vector A of length n. I would like to create a new vector B which contains each element of A repeated m times; i.e. length of B is mxn. For example, let A=[2 4 5 7 8]; m=3; then, B=[2 2 2 4 4 4 5 5 5 7 7 7 8 8 8]; What is the fastest way of doing that? Thanks!

Respuesta aceptada

Star Strider
Star Strider el 23 de Feb. de 2016
Two options, both producing the same output:
A=[2 4 5 7 8];
B1 = repelem(A,3) % Introduced in R2015a
B2 = reshape(repmat(A, 3, 1), [], 1)'
B1 =
2 2 2 4 4 4 5 5 5 7 7 7 8 8 8
B2 =
2 2 2 4 4 4 5 5 5 7 7 7 8 8 8

Más respuestas (1)

Walter Roberson
Walter Roberson el 23 de Feb. de 2016
B = reshape([A;A;A], 1, [])
  1 comentario
Mnr
Mnr el 23 de Feb. de 2016
Thanks! but what about for any m? In other words, how can I write reshape([A;A;A]) for m>3?

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices 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