I have written a code to generate a matrix with four columns to get all combinations of numbers whose sum is equal to 9 and each number varies from 0 to 9.
m = zeros(220, 4);
pd = 9;
i = 1;
for p = 0:1:pd
for q = 0:1:pd-p
for a = 0:1:pd-q-p
m(i,:) = [p, q, a, pd-a-q-p];
i = i+1;
end
end
end
m
Now i want filter the arrays with no zero, one zero, two zeros, three zeros. Like, Three zero case
0 0 0 9
Two zero case
0 0 1 8
0 0 2 7
.
.
0 0 8 1
One zero case
0 1 1 7
0 1 2 6
.
.
.
0 7 1 1
and no zero case
1 1 1 6
1 1 2 5
.
.
6 1 1 1
and so on..
Any suggestions to do that or any alternative method ?
Update:
0 0 0 9
0 0 1 8
0 0 2 7
.
.
0 0 8 1
0 1 1 7
0 1 2 6
.
.
.
0 7 1 1
1 1 1 6
1 1 2 5
.
.
6 1 1 1
Any suggestions to get the matrix m
in the above order?