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

1 次查看(过去 30 天)
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 个评论
Lucas Medina
Lucas Medina 2015-7-9
编辑:Lucas Medina 2015-7-9
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.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2015-7-9
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 个评论
Lucas Medina
Lucas Medina 2015-7-9
Exactly what I wanted. Thanks. Why wouldn't you recommend inv? How else would you take the inverse of a matrix?

请先登录,再进行评论。

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2015-7-9
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))

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by