Issue with 3Sum problem!
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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일
0 개 추천
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일
0 개 추천
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)
