Adding all possible combinations of array elements

I have an array of elements,
How would I generate a new array of all possible combinations, multiples, and combinations of multiples within a threshold limit of, say, 30?
ex:
X=[a b c d]; where a, b, c, d are real, positive, small integers
M=[a, b, c, d, 2*a, 2*b, ... 3*a, 3*b,...4*a, 4*b, ...a+b, a+b+c,... 2a+2b,...2a+c,... 10a+10b+d, ... .. .. ]
M(M>=30)=[];
where ALL possible combinations are there, excluding repeat combinations, but including repeat numbers

댓글 수: 3

Image Analyst
Image Analyst 2014년 8월 12일
편집: Image Analyst 2014년 8월 12일
Well I think that would be an infinite number of numbers, unless you put on some constraints, like the multiplication factors must be positive integers or something.
Is this homework? It kind of sounds like homework.
And do you need to keep track of the multiplication factors that were used to create each number?
Hey, are you still there?
K
K 2014년 8월 12일
Hey, I'm still here. It's not homework, it is a simulation program to help model data.
There are definitely constraints. They are all positive, real numbers, that have larger spacing between them as I to the right on the array. I don't need to keep track of the multiplication factor as I go.
I was thinking to first make a matrix of the multiplication factors, where I have my array accross the top line, then all the multiples below, then deleting the numbers greater than my limit. Like so:
a b c d
2a 2b 2c 2d
3a 3b.....
Then doing all the number combinations after that... ?

댓글을 달려면 로그인하십시오.

답변 (1개)

Ahmet Cecen
Ahmet Cecen 2014년 8월 12일

0 개 추천

Use combnk to get the the unique combinations over a,b,c and d. For each of those combinations, use base math to get the repetitive variations from 1 to 30. However, the number of elements within M might explode at 30. You might want to consider a lower number.

댓글 수: 4

Thank you. Using combnk is working great, the only problem is, is that I want to sum across the rows of each combnk matrix I am generating for different sizes. My coding, is like so:
for k=2:4
C = combnk(B,k);
C1=sum(C,2);
C1(C1>E_avail)=[]
end
Here, B is an array. when I step through the loop, everything looks right. But I want to keep the data, so I tried making C into C(k). This gives me the error message:
"In an assignment A(I) = B, the number of elements in B and I must be the same."
Is there a good way to do this?
Use cell structure to keep the C's. C{i} with curly brackets.
K
K 2014년 8월 13일
This gives me the error:
"Cell contents assignment to a non-cell array object."
Is there another way?
K
K 2014년 8월 13일
Actually, i just got it working. Thank you for your advice. The curly brackets work wonderfully. I just had to create a cell array outside the for loop for it to input into.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

K
K
2014년 8월 12일

댓글:

K
K
2014년 8월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by