How does matlab get the combinations that meet certain conditions in the matrix
4 次查看(过去 30 天)
显示 更早的评论
Suppose I have a matrix A = [1 2 3 4 5 6 7 8 9 10]] I want to find out the combination of the following conditions respectively how can I achieve 2A1 = A2 A1 + A2 = A3
2 个评论
Jan
2022-12-10
What do you call "A1", "A2" and "A3"? Do you mean A(1), A(2), A(3)? Then you have written the solution already...
This sounds like a homework question. Then please post, what you have tried so far and ask a specific question. The forum will not solve your homework.
采纳的回答
Walter Roberson
2022-12-10
A = [1 2 3 4 5 6 7 8 9 10];
[A1, A2, A3] = ndgrid(A);
dA1 = 2*A1;
mask = dA1 == A3 & dA1 == A2.*A1 + A2;
solutions = [A1(mask), A2(mask), A3(mask)]
Yup, that works.
As the size() of A goes up, or as the number of variables involves increases, then the memory requirements can go up a lot for this approach, and you start to need other approaches.
更多回答(2 个)
Bruno Luong
2022-12-10
编辑:Bruno Luong
2022-12-11
You don't need MATLAB at all. From
2A1 = A2 A1 + A2,
We divide by A1, and factor A2 on rhs to get
2 = A2*(1+1/A1)
Meaning 2 is integer multiple of (1+1/A1).
But (1+1/A1) > 1 (since A1 > 0), or in other world
(1+1/A1) > 2/2 > 2/3 > 2/4 .... > 2/10
So A2 must be 1 (we just exclude A2 to be 2, 3, 4, ... 10)
and (1+1/A1) must be 2. Therefore A1 = 1, and A2=1, A3=2.
0 个评论
John D'Errico
2022-12-10
编辑:John D'Errico
2022-12-10
Assuming you want to solve this problem:
2*A1 = A2
A1 + A2 = A3
then you could use the ndgrid solution, as shown by Walter. In fact, that is surely the solution for such a small problem, since there are only 1000 possible combinations.
Of course, on this specific problem, the answer is simple. Replace a2 in the second equation, and we see
A1 + 2*A1 = A3
So we now have two euations:
A2 = 2*A1
A3 = 3*A1
So we can choose any value for A1 that will not cause a problem with A3. Thus we trivially have
A1 = 1:3
A2 = 2*A1
A3 = 3*A1
That is clearly the set of all possible solutions. Is this always a viable approach? If the system were far more complex, of course there could be problems. For example, suppose you have some more complex system? I chose a random set of numbers here.
syms x y z
A = [1 1 3
4 4 2];
B = [27;36]
Now suppose we want to find all solutions of this form
A*[x;y;z] == B
where (x,y,z) all come from that set? Clearly, I have no clue as to whether such a solution even exists. We can find if a particular solution exists. For example, intlinprog will do it.
xyz = intlinprog(ones(3,1),[1 2 3],[],[],A,B,ones(3,1),ones(3,1)*10)
Now we know that no solution exists at all, for that problem. If I change B, though,
B2 = [20;30]
xyz = intlinprog(ones(3,1),[1 2 3],[],[],A,B2,ones(3,1),ones(3,1)*10)
then we do find a solution. But not all possible solutions.
format rat
[R,basis] = rref([A,B2])
which tells us the problem reduces to
z == 5, and
x + y == 5
And now we can trivially find all solutions from the admissable set.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!