MATLAB Answers

0

List(Vecor) Generation

Asked by Junjie Liao on 8 Sep 2019
Latest activity Edited by Andrei Bobrov
on 8 Sep 2019
Can I generate list [1 2 3 2 3 4 3 4 5 4 5 6 ....] without for loop?
Thanks!

  0 Comments

Sign in to comment.

3 Answers

madhan ravi
Answer by madhan ravi
on 8 Sep 2019
 Accepted Answer

m=3; % pairs
n=4; % n combinations
reshape((0:n-1)+(1:m)',1,[])

  1 Comment

madhan ravi
on 8 Sep 2019
If you're using version prior to 2016b:
% last line would be
reshape(bsxfun(@plus,(0:n-1),(1:m)'),1,[])

Sign in to comment.


Walter Roberson
Answer by Walter Roberson
on 8 Sep 2019

T=1:3*n;
Vec = floor(T/3) + mod(T-1,3) + 1;

  2 Comments

Junjie Liao on 8 Sep 2019
The output is 1 2 4 2 3 5 3 4 6 :(
Junjie Liao on 8 Sep 2019
Correct answer should be Vec = floor(T/4) + mod(T-1,3) + 1.
Thanks!

Sign in to comment.


Andrei Bobrov
Answer by Andrei Bobrov
on 8 Sep 2019
Edited by Andrei Bobrov
on 8 Sep 2019

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);

  0 Comments

Sign in to comment.