Vectorization with a loop and repmat

19 visualizaciones (últimos 30 días)
yoshiyuki
yoshiyuki el 14 de Ag. de 2014
Respondida: Matt J el 14 de Ag. de 2014
i would like to vectoralize the following codes with loop;
n=5;
k=3;
x=(k:n)';
m=[];
for i = 1 : (n-k+1)
m=[m,x];
x=circshift(x,-1);
end
example;
k=3, n=5
3 4 5,
4 5 3,
5 3 4
please help me out

Respuesta aceptada

Michael Haderlein
Michael Haderlein el 14 de Ag. de 2014
That sounds like a nice cody task ;-) I'm sure there are dozens of solution, so my suggestion is:
m=k+mod(bsxfun(@(x,y) y+x,(0:n-k)',0:n-k),n-k+1);
  1 comentario
yoshiyuki
yoshiyuki el 14 de Ag. de 2014
Michael Thanks a lot for your elegant answer!

Iniciar sesión para comentar.

Más respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 14 de Ag. de 2014
Editada: Azzi Abdelmalek el 14 de Ag. de 2014
n=5;
k=3;
x=(k:n)'
idx=rem(bsxfun(@plus,(0:n-k)',(0:n-k)),n-k+1)+1
out=x(idx)

Matt J
Matt J el 14 de Ag. de 2014
y=k:n-1;
m = fliplr(toeplitz([n,y],[n,fliplr(y)]))

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by