How to instantly substitute all the Ai_j of a symbolic matrix A?

1 visualización (últimos 30 días)
I know that I can create a symbolic matrix A in the following way:
>> A = sym('A', [3 3], 'real')
A =
[ A1_1, A1_2, A1_3]
[ A2_1, A2_2, A2_3]
[ A3_1, A3_2, A3_3]
Then I can, for example create a matrix representing the inverse of A:
>> Ai = inv(A)
Ai =
[ (A2_2*A3_3 - A2_3*A3_2)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), -(A1_2*A3_3 - A1_3*A3_2)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), (A1_2*A2_3 - A1_3*A2_2)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1)]
[ -(A2_1*A3_3 - A2_3*A3_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), (A1_1*A3_3 - A1_3*A3_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), -(A1_1*A2_3 - A1_3*A2_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1)]
[ (A2_1*A3_2 - A2_2*A3_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), -(A1_1*A3_2 - A1_2*A3_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1), (A1_1*A2_2 - A1_2*A2_1)/(A1_1*A2_2*A3_3 - A1_1*A2_3*A3_2 - A1_2*A2_1*A3_3 + A1_2*A2_3*A3_1 + A1_3*A2_1*A3_2 - A1_3*A2_2*A3_1)]
Say I wanted to evaluate Ai for [1 2 3; 5 4 6; 7 8 9]. How can I substitute all the Ai_j without using subs() one by one on each entry in A? I'd like to be able to substitute the new A as a whole without having to substitute each of its entries.
  1 comentario
Lucas Medina
Lucas Medina el 9 de Jul. de 2015
Editada: Lucas Medina el 9 de Jul. de 2015
I used those numbers as an example. What I actually want to do is work with random matrices of varying dimensions, which means I won't know what the values are or how many to put in. That approach would be useless in this case.

Iniciar sesión para comentar.

Respuesta aceptada

Steven Lord
Steven Lord el 9 de Jul. de 2015
SUBS lets you specify a symbolic matrix for substitution. See the description of the input argument named "old" (particularly the types of inputs accepted for that argument) in the Inputs section of its documentation page.
A = sym('A', [3 3], 'real');
B = A^2; % I don't recommend using INV, so I'll use squaring instead
M = reshape(randperm(9), 3, 3); % arbitrary data to be substituted
s1 = subs(B, A, M);
s2 = M^2;
isequal(double(s1), s2) % should return true
  1 comentario
Lucas Medina
Lucas Medina el 9 de Jul. de 2015
Exactly what I wanted. Thanks. Why wouldn't you recommend inv? How else would you take the inverse of a matrix?

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 9 de Jul. de 2015
A = sym('A', [3 3])
Ai=inv(A)
a=[1 2 3; 5 4 6; 7 8 9]
arrayfun(@(x,y) assignin('base',char(x),y),A,a)
double(subs(Ai))

Community Treasure Hunt

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

Start Hunting!

Translated by