create vector of repeating elements (sort of)
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Leor Greenberger
el 20 de Sept. de 2011
Comentada: HC Song
el 1 de Jun. de 2017
How can I easily create a vector such that:
w = 3
n = 8
v = (1 w times) (2 w times) (3 w times) ... (n w times)
so i would have:
v = [1 1 1 2 2 2 3 3 3 4 4 4 .... 8 8 8]
0 comentarios
Respuesta aceptada
Más respuestas (4)
Richard Tyson
el 15 de Ag. de 2013
If you need it to be fast you should avoid using repmat. Stick to C functions which don't need to parse input arguments and do one specific task:
n = 8;
w = 3;
v = ceil( [1:(w*n)]./w )
If anyone has a faster way please post. I do this kind of operation a lot.
2 comentarios
the cyclist
el 15 de Ag. de 2013
This thread is nearly two years old. You might want to post this as a new question.
Artur Palha
el 25 de Sept. de 2014
n = 8; w = 3; v = rectpulse(1:n)
This is the fastest option I know of.
Lucas García
el 20 de Sept. de 2011
One of many ways:
>> w = 3; n = 8;
>> v = repmat(1:n,w,1);
>> v = v(1:end)
0 comentarios
Wayne King
el 20 de Sept. de 2011
One way:
w = 3;
x = 1:8;
x = arrayfun(@(x) repmat(x,1,w),x,'UniformOutput',false);
x = cell2mat(x);
Wayne
1 comentario
Jan
el 20 de Sept. de 2011
ARRAYFUN and CELL2MAT needs a lot of time. Using REPMAT directly is much faster.
Ver también
Categorías
Más información sobre Data Types en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!