List of increasing integers

2 views (last 30 days)
Tony Haines
Tony Haines on 22 Feb 2024
Commented: Dyuman Joshi on 22 Feb 2024
I have a list of dimensions given by the row vector as follows
I would like to associate with this vector, a list of increasing integers such that each size in 'dim' corresponds with an exact number of integers like this:
int = [ 1 2 , 3 4, 5 6 7 8, 9 10, 11 12, 13 14, 15 16, 17 18, 19 20, 21 22, 23 24, 25 26]
I grouped the integers just to indicate that a dim=4 corresponds to 5 6 7 8. and the rest just has two integers.
Thanks.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 22 Feb 2024
dim = [2 2 4 2 2 2 2 2 2 2 2 2]
dim = 1×12
2 2 4 2 2 2 2 2 2 2 2 2
int = 1:sum(dim)
int = 1×26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
out = mat2cell(int, 1, dim)
ans = 1×12 cell array
{[1 2]} {[3 4]} {[5 6 7 8]} {[9 10]} {[11 12]} {[13 14]} {[15 16]} {[17 18]} {[19 20]} {[21 22]} {[23 24]} {[25 26]}
  2 Comments
Tony Haines
Tony Haines on 22 Feb 2024
This is what I needed. To have it all in a cell. Thank you!

Sign in to comment.

More Answers (1)

Les Beckham
Les Beckham on 22 Feb 2024
Are you sure that is what you want? It doesn't seem very useful.
dim = [2 2 4 2 2 2 2 2 2 2 2 2]
dim = 1×12
2 2 4 2 2 2 2 2 2 2 2 2
int = 1:sum(dim)
int = 1×26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
int(end)
ans = 26
  1 Comment
Tony Haines
Tony Haines on 22 Feb 2024
Thank you very much. It's very useful for what i'm working on.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!