Hi guys, this is my final questions. So the objective of this program is to find all combinations of a, b, and c that equal zero. I want it to keep spitting out combinations until it's found all possible ones, but right now if I run it, it will either not get one and stop, or it will get a few and then stop. Here is the code:
F = [-5,-4,-3,-2,-1,1,2,3,4,5];
T = @(a,b,c) (a + b + c);
for m=F
a = randsample (F,1);
b = randsample (F,1);
c = randsample (F,1);
if T(a,b,c) == 0
disp ([a b c]);
end
end
Thanks for the help in advance.

댓글 수: 2

per isakson
per isakson 2012년 4월 29일
Loop loops ten loops, but with ten there is a probability that no combination of a, b and c fulfills the condition.
Nathaniel Ewing
Nathaniel Ewing 2012년 4월 29일
How do I fix this so it repeats the loops if it doesn't full the condition?

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

 채택된 답변

per isakson
per isakson 2012년 4월 29일

0 개 추천

M = [];
for ii = 1 : large_number
.....
if T(a,b,c)==0
M = cat( 1, M, [a,b,c] );
end
end
M = unique( M, 'rows' );
--- EDIT ---
function U = cssm( N )
F = [-5,-4,-3,-2,-1,1,2,3,4,5];
T = @(a,b,c) (a + b + c);
M = [];
for m = 1 : N
a = randsample (F,1);
b = randsample (F,1);
c = randsample (F,1);
if T(a,b,c) == 0
M = cat( 1, M, [a,b,c] );
end
end
U = unique( M, 'rows');
end
You decide what all combinations mean.
U = cssm(1e4);
1e4 is that large enough to capture all combinations?

댓글 수: 2

Nathaniel Ewing
Nathaniel Ewing 2012년 4월 29일
I take it the dots are where I put my original code?
Nathaniel Ewing
Nathaniel Ewing 2012년 4월 29일
I'm not sure how I would use the M in this situation, or Concatenate arrays. Could you explain a bit please? Thanks!

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

추가 답변 (1개)

Nathaniel Ewing
Nathaniel Ewing 2012년 4월 29일

0 개 추천

Yes this works perfectly, thank you very much for your help.

Community Treasure Hunt

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

Start Hunting!

Translated by