How do I generate all quadruplets?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Dear all,
How can I generate all quadruplets
where
and satisfy the following conditions
and
?.
채택된 답변
John D'Errico
2022년 11월 12일
편집: John D'Errico
2022년 11월 12일
You can't. Period. There are infinitely many such points. Can you generate infinitely many points? Can you store them all? Is your computer fast enough to do infinitely many computations? Even if you store only those that are possible in double precision, you don't have enough memory. I don't care how big or fast is your computer. Not possible. Of course, I cannot know what you mean by [0,1]. Is that a set of size 2, where each element is an binary integer? Or does that refer to the interval [0,1], where we have continuous variables?
If this is a binary integer thing, where each variable is restricted to ONLY 0 or 1, then it is trivial.
[a,b,c,d] = ndgrid([0,1],[0,1],[0,1],[0,1]);
retain = (a<=b) & (c<=d) & (a+c<=1) & (b+d<=1);
abcd = [a(retain),b(retain),c(retain),d(retain)]
abcd = 5×4
0 0 0 0
0 1 0 0
1 1 0 0
0 0 0 1
0 0 1 1
If that is your goal, then your answer lies in the columns of abcd.
댓글 수: 5
Rajkumar Verma
2022년 11월 12일
Thanks for your answer. I am agree with you.
Suppose if I take
then is it possible?
Ah. so I was right the first time. You WERE asking about entire intervals with continuous variables.
If you want to use discrete levels, then
11^4
ans = 14641
is not that large.
What did I do with ndgrid? Is there any reason why you could not do as I showed how to do with TWO level variables? The problem is, you will now run into floating point arithmetic issues, because those levels are not exactly representable in floating point arithmetic.
What this means is you need to do all of the comparisons using INTEGER arithmetic.
levels = 0:1:10;
[a,b,c,d] = ndgrid(levels,levels,levels,levels);
retain = (a<=b) & (c<=d) & (a+c<=10) & (b+d<=10);
abcd = [a(retain),b(retain),c(retain),d(retain)]/10
abcd = 1001×4
0 0 0 0
0 0.1000 0 0
0.1000 0.1000 0 0
0 0.2000 0 0
0.1000 0.2000 0 0
0.2000 0.2000 0 0
0 0.3000 0 0
0.1000 0.3000 0 0
0.2000 0.3000 0 0
0.3000 0.3000 0 0
So ONLY divide by 10 AFTER the integer computations are performed. There are exactly 1001 possible combinations. Don't go too far of course, as it is just too easy for people to think their computer is infinitely large and infinitely fast.
Another problem is the above scheme is essentially a rejection method. It generates 14641 initial sets, then discards over 90% of them to end up with only 1001 sets in the acceptable set. And rejection methods like this can easily overwhelm your computer is you were to go too far. So if you were to chose 10001 levels for each variable, you would surely then expect memory problems.
Rajkumar Verma
2022년 11월 12일
Thanks you so much for your guidence. I have solve my problem by using your idea.
levels = 0:.3:1;
[a,b,c,d] = ndgrid(levels ,levels ,levels ,levels );
retain = (a<=b) & (c<=d) & (a+c<=1) & (b+d<=1);
M = [a(retain),b(retain),c(retain),d(retain)];
%%%%%%%%%%%%%%%%%%%%
Now I want to use the following formula for obtained all quadruplets
You did not pay attention to what I said. First of all, this does not even sample the entire interval, up to 1.
levels = 0:.3:1
levels = 1×4
0 0.3000 0.6000 0.9000
It never gets up to 1.
Next, you need to understand floating point arithmetic. There was a good reason I said NOT to do what you want to do!
The number 0.3 is NOT exactly representable as a double. Consider this next test:
0.3 + 0.3 + 0.3 == 0.9
ans = logical
0
Do you see that it returns a logical false?
Again, you need to operate on INTEGERS. I said that for a reason, as surely I was not just wanting my time typing for the fun of it.
So you MIGHT do this:
levels = 0:3
levels = 1×4
0 1 2 3
[a,b,c,d] = ndgrid(levels ,levels ,levels ,levels );
retain = (a<=b) & (c<=d) & (a+c<=3) & (b+d<=3);
M = [a(retain),b(retain),c(retain),d(retain)]/3
M = 35×4
0 0 0 0
0 0.3333 0 0
0.3333 0.3333 0 0
0 0.6667 0 0
0.3333 0.6667 0 0
0.6667 0.6667 0 0
0 1.0000 0 0
0.3333 1.0000 0 0
0.6667 1.0000 0 0
1.0000 1.0000 0 0
The 35 quaduplets as computed now will be what you might wanted to see.
Next, you are asking how to compute firther operations. I said, the result contains a,b,c,d as columns of the result. Or, if you wanted to end up with vectors of results, you could just have done:
a = a(retain);
b = b(retain);
c = c(retain);
d = d(retain);
Now you can compute anything you want to do with them.
Don't forget to use the dotted .* and ./ and .^ operators when you work with vectors of numbers and you want to do element-wise oiperations.
Rajkumar Verma
2022년 11월 12일
Thank you so much for your valuable suggestons.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
태그
참고 항목
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)
