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

1 view (last 30 days)
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 Comment
Lucas Medina
Lucas Medina on 9 Jul 2015
Edited: Lucas Medina on 9 Jul 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.

Sign in to comment.

Accepted Answer

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

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 9 Jul 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!