How does matlab get the combinations that meet certain conditions in the matrix

4 visualizaciones (últimos 30 días)
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 comentarios
Jan
Jan el 10 de Dic. de 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.
peter huang
peter huang el 11 de Dic. de 2022
Sorry, because I have a huge amount of data during the process, so I can only use this method. I want to achieve my expected effect through a simple example

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Dic. de 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)]
solutions = 1×3
1 1 2
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.

Más respuestas (2)

Bruno Luong
Bruno Luong el 10 de Dic. de 2022
Editada: Bruno Luong el 11 de Dic. de 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.

John D'Errico
John D'Errico el 10 de Dic. de 2022
Editada: John D'Errico el 10 de Dic. de 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
A1 = 1×3
1 2 3
A2 = 2*A1
A2 = 1×3
2 4 6
A3 = 3*A1
A3 = 1×3
3 6 9
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]
B = 2×1
27 36
Now suppose we want to find all solutions of this form
A*[x;y;z] == B
ans = 
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)
LP: Optimal objective value is 12.600000. No feasible solution found. Intlinprog stopped because no integer points satisfy the constraints. xyz = []
Now we know that no solution exists at all, for that problem. If I change B, though,
B2 = [20;30]
B2 = 2×1
20 30
xyz = intlinprog(ones(3,1),[1 2 3],[],[],A,B2,ones(3,1),ones(3,1)*10)
LP: Optimal objective value is 10.000000. Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).
xyz = 3×1
4.0000 1.0000 5.0000
then we do find a solution. But not all possible solutions.
format rat
[R,basis] = rref([A,B2])
R =
1 1 0 5 0 0 1 5
basis =
1 3
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.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by