How does matlab get the combinations that meet certain conditions in the matrix
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
peter huang
le 10 Déc 2022
Commenté : peter huang
le 11 Déc 2022
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 commentaires
Jan
le 10 Déc 2022
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.
Réponse acceptée
Walter Roberson
le 10 Déc 2022
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.
Plus de réponses (2)
Bruno Luong
le 10 Déc 2022
Modifié(e) : Bruno Luong
le 11 Déc 2022
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 commentaires
John D'Errico
le 10 Déc 2022
Modifié(e) : John D'Errico
le 10 Déc 2022
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 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!