Vectorization with a loop and repmat
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Michael Haderlein
2014-8-14
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);
更多回答(2 个)
Azzi Abdelmalek
2014-8-14
编辑:Azzi Abdelmalek
2014-8-14
n=5;
k=3;
x=(k:n)'
idx=rem(bsxfun(@plus,(0:n-k)',(0:n-k)),n-k+1)+1
out=x(idx)
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!