Dear all,
How can I generate all quadruplets where and satisfy the following conditions and ?.

 Akzeptierte Antwort

John D'Errico
John D'Errico am 12 Nov. 2022
Bearbeitet: John D'Errico am 12 Nov. 2022

0 Stimmen

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 Kommentare

Rajkumar Verma
Rajkumar Verma am 12 Nov. 2022
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
Rajkumar Verma am 12 Nov. 2022
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
. How can I calculate it?
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
Rajkumar Verma am 12 Nov. 2022
Thank you so much for your valuable suggestons.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by