Issue with 3Sum problem!
显示 更早的评论
Alright so I took some stuff out and now I'm down to this. For some reason though, it's just coming up with random numbers from the array instead of using them to find a 1D array which adds to zero. I think what the issue is, is that it's not using the integers to try and find where T would equal zero.
F = [-5,-4,-3,-2,-1,1,2,3,4,5];
for T = @(a,b,c) (a + b + c);
a = randsample (F,1);
b = randsample (F,1);
c = randsample (F,1);
if T(a,b,c) == 0
disp [a b c]
end
end
采纳的回答
Your line
for T = @(a,b,c) (a + b + c)
does not really specify a loop. What you want is to find some a, b, and c whose sum is 0, so you need to loop through a, b, c. For example, if you want to do it 10 times, you can do
F = [-5,-4,-3,-2,-1,1,2,3,4,5];
T = @(a,b,c) (a + b + c);
for m = 1:10
a = randsample (F,1);
b = randsample (F,1);
c = randsample (F,1);
if T(a,b,c) == 0
disp([a b c])
end
end
11 个评论
Note that
disp [a b c]
means
disp('[a', 'b', 'c]')
Consider instead using
disp([a b c])
Thanks Walter, didn't pay attention to that. I updated the post.
Okay, so I have all of that fixed. I tried using a while loop to get it to repeat until it hit the right numbers instead of having to repeatedly hit it, but that doesn't work. Last question I swear: how would I have that automatically repeat instead of requiring my input?
Please show us your attempt with a while loop, and tell us what did not work about it.
F = [-5,-4,-3,-2,-1,1,2,3,4,5];
T = @(a,b,c) (a + b + c);
m=F;
while (0 > T(a,b,c) > 0)
a = randsample (F,1);
b = randsample (F,1);
c = randsample (F,1);
if T(a,b,c) == 0
disp ([a b c])
break
end
end
For some reason it's just not looping like it should, I still have to click it constantly until it gets a solution.
0 > T(a,b,c) > 0
would be interpreted as
((0 > T(a,b,c) > 0)
The first subexpression is a logical comparison and so would return 0 (false) or 1 (true). That 0 or 1 would then (second subexpression) be compared to 0; 0 or 1 is greater than 0 only if the first subexpression is 1 (true). But then you can see that that is a redundant logical condition, the same as
0 > T(a,b,c)
I am not sure what you are trying to test there. Loop until you find a 0 value? But your existing "break" condition would already exit the loop when a match is found, so testing for non-zero would be unnecessary. You might as well just loop forever:
while true
Note with your existing code, you are going to have problems because the "while" is using T(a,b,c) before a, b, or c have been given values.
Yes I'm trying to loop until I find a zero value. I removed the break and supressed the disp ([a b c]) which seemed to help, it's now displaying multiple answers which is partially what I was looking for. I just need the automation part now.
If you commented out the disp() then there should be nothing left there that can display multiple values.
If it is already displaying multiple answers, then how does that differ from the automation you are looking for?
If you want multiple answers displayed, but you also want to stop at the first answer, that seems to be a contradiction.
When I look at another question (by someone else) on the same topic, I find that _they_ were required to produce the entire list of solutions. Is that a requirement for you as well? If it is then you are going about it the wrong way.
Yes it's supposed to be a list of solutions, which I can't seem to get.
What will happen is it will display two sets of numbers once and awhile if it finds them, but otherwise I have to keep clicking until it does so.
更多回答(1 个)
Nathaniel Ewing
2012-4-29
You guys are terrific, thank you for helping me learn!
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
另请参阅
2012-4-28
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
