How to impose a condition creating a matrix.

2 views (last 30 days)
I want to create a matrix with all the possible combinations of 10 numbers between 0 and 100, with intervals of 5, that its sum be equal to 100. I mean something like this:
(0 0 0 0 0 0 0 0 0 10 90; 10 10 10 10 10 10 10 10 20 0;...)
I use "allcomb.m" to create something like all the possible numbers that are between 0 and 100, with intervals of 5. However, this matrix is so big, and that implies that Matlab doesn't create it. I was thinking that, if I have that matrix, I could reduce it using a condition but this is impossible because I never get the matrix. So, the question is how I can modify the allcomb's code with the condition in the same code or maybe, and better, another way to create the matrix that I purpose.
Thanks.

Accepted Answer

Iman Ansari
Iman Ansari on 19 Apr 2013
Edited: Iman Ansari on 19 Apr 2013
Hi. With 0:5:100 got out of memory error:
n=0:10:100;
A=n';
for i=1:9
x=repmat(n,[size(A,1) 1]);
A=repmat(A,[numel(n) 1]);
x=x(:);
A=[A x];
A=A(sum(A,2)<=100,:);
end
A=A(sum(A,2)==100,:);
  1 Comment
Iván López
Iván López on 19 Apr 2013
Edited: Iván López on 19 Apr 2013
Thank you, Iman Ansari. Fortunately I can use a powerfull hardware where I could use 0:5:100. Thanks for your time.

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!