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

 채택된 답변

Michael Haderlein
Michael Haderlein 2014년 8월 14일

1 개 추천

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

yoshiyuki
yoshiyuki 2014년 8월 14일
Michael Thanks a lot for your elegant answer!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 14일
편집: Azzi Abdelmalek 2014년 8월 14일

0 개 추천

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 2014년 8월 14일

0 개 추천

y=k:n-1;
m = fliplr(toeplitz([n,y],[n,fliplr(y)]))

카테고리

도움말 센터File Exchange에서 Just for fun에 대해 자세히 알아보기

제품

태그

질문:

2014년 8월 14일

답변:

2014년 8월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by