61 views (last 30 days)

I want to construct a matrix, m x n, filled with the numbers 1 till 5. The numbers have to be successive, and have to be increasing. One column of the matrix could for example look like.

A =1 1 2 2 2 2 3 4 5 5 5 5

or

A = 1 2 3 3 3 4 5 5 5 5 5 5

I want to construct a matrix with all possible options. The first column of matrix A therefore should look like this:

A(:,1) = 1 1 1 1 1 1 1 1 2 3 4 5

and the last one like this

A(:,end)= 1 2 3 4 5 5 5 5 5 5 5

In my case, the matrix will have a length of 96, instead of the above example where the length is 12. Could you help me?

Bruno Luong
on 25 Jan 2021

Edited: Bruno Luong
on 25 Jan 2021

p = 5;

n = 12;

j = nchoosek(2:n,p-1);

m = size(j,1); % == nchoosek(n-1,p-1) == 330 and not 96

i = repmat((1:m)',1,p-1);

A = cumsum(accumarray([i(:) j(:)],1,[m n]),2)+1

Adam Danz
on 25 Jan 2021

Nice one (+1)

When n==96, A has 3,183,545 rows and takes less than 3 sec using the same machine as above.

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

Start Hunting!
## 8 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285528

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285528

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285613

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285613

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285633

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285633

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285643

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285643

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285663

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285663

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285673

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285673

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285683

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285683

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285738

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/726713-fill-matrix-with-all-options-of-successive-increasing-numbers-1-5#comment_1285738

Sign in to comment.