List(Vecor) Generation

4 ビュー (過去 30 日間)
Junjie Liao
Junjie Liao 2019 年 9 月 8 日
編集済み: Andrei Bobrov 2019 年 9 月 8 日
Can I generate list [1 2 3 2 3 4 3 4 5 4 5 6 ....] without for loop?
Thanks!

採用された回答

madhan ravi
madhan ravi 2019 年 9 月 8 日
m=3; % pairs
n=4; % n combinations
reshape((0:n-1)+(1:m)',1,[])
  1 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 8 日
If you're using version prior to 2016b:
% last line would be
reshape(bsxfun(@plus,(0:n-1),(1:m)'),1,[])

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2019 年 9 月 8 日
T=1:3*n;
Vec = floor(T/3) + mod(T-1,3) + 1;
  2 件のコメント
Junjie Liao
Junjie Liao 2019 年 9 月 8 日
The output is 1 2 4 2 3 5 3 4 6 :(
Junjie Liao
Junjie Liao 2019 年 9 月 8 日
Correct answer should be Vec = floor(T/4) + mod(T-1,3) + 1.
Thanks!

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2019 年 9 月 8 日
編集済み: Andrei Bobrov 2019 年 9 月 8 日
m=3;
n=4;
T = 1:m*n;
out = ceil(T/m) + mod(T-1,m);
or
out = floor(T/n) + mod(T-1,m) + 1;
or
out = repmat(1:m,1,n) + repelem(0:n-1,m);

カテゴリ

Help Center および File ExchangeDijkstra algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by