Vectorization with a loop and repmat

19 ビュー (過去 30 日間)
yoshiyuki
yoshiyuki 2014 年 8 月 14 日
回答済み: Matt J 2014 年 8 月 14 日
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 日
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 日
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 日
y=k:n-1;
m = fliplr(toeplitz([n,y],[n,fliplr(y)]))

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by