How do I generate all quadruplets?

3 次查看(过去 30 天)
Dear all,
How can I generate all quadruplets where and satisfy the following conditions and ?.

采纳的回答

John D'Errico
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 个评论
John D'Errico
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
Rajkumar Verma 2022-11-12
Thank you so much for your valuable suggestons.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by