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
採用された回答
Honglei Chen
2012 年 4 月 29 日
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 件のコメント
Walter Roberson
2012 年 4 月 29 日
Note that
disp [a b c]
means
disp('[a', 'b', 'c]')
Consider instead using
disp([a b c])
Honglei Chen
2012 年 4 月 29 日
Thanks Walter, didn't pay attention to that. I updated the post.
Nathaniel Ewing
2012 年 4 月 29 日
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?
Walter Roberson
2012 年 4 月 29 日
Please show us your attempt with a while loop, and tell us what did not work about it.
Nathaniel Ewing
2012 年 4 月 29 日
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
Nathaniel Ewing
2012 年 4 月 29 日
For some reason it's just not looping like it should, I still have to click it constantly until it gets a solution.
Walter Roberson
2012 年 4 月 29 日
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.
Nathaniel Ewing
2012 年 4 月 29 日
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.
Walter Roberson
2012 年 4 月 29 日
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.
Nathaniel Ewing
2012 年 4 月 29 日
Yes it's supposed to be a list of solutions, which I can't seem to get.
Nathaniel Ewing
2012 年 4 月 29 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
