How do I generate all quadruplets?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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?
John D'Errico
2022 年 11 月 12 日
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
John D'Errico
2022 年 11 月 12 日
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!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)
