Preserve the order of matrix when doing symbolic matrix multiplication

Hi community,
I have been trying to use Matlab to do a large matrix multiplication using the symbolic tool. However, I found that the result it gave me is confusing in the order of matrices. Searched the mathwork forums and found a similar question without anwers:
So I decided to raise a new one and see if anyone knows the answer.
Problem I encounter is,
I define symbolic matrix as
A = sym('A', [1,1]);
B = sym('B',[1,1]);
and calculate
C = B*A,
and matlab will give me the answer C = A01*B01 which isn't the right order. The problem seems to root in the fact that matlab see the symbols as scalars when defined the dimension of [1,1], but I have to do this as my entire matrix is too big (38*38) so if I do dimension more than 1, it is very difficult to extract the matrix from the result.
Is there a way to force the order when doing multiplication? If not, could someone suggest other tools that can do this please?
Thanks in advance!

 Risposta accettata

This is not possible in any released versions.
It might be possible in a future version.
In all released versions, individual symbolic variables are always treated as scalars, and then the internal sorting rules designed to detect duplicate expressions come into play to re-order the names.

5 Commenti

Yeah I guessed so too. And it seems that one of the internal sorting rules is the alphabetical order from left to right.
Hope the mathwork development team will add this feature into future releases!
Thank you so much for your answer.
My tests a second ago suggest that variables in calculation are sorted according to ascii order:
  • if two names have compared equal so far, but one of the names has reached the end of the name and the other has not, then the shorter name goes first ("end" marker sorts before anything else at the same position)
  • in any one character position, a digit sorts before an upper-case letter
  • an upper-case letter sorts before an underscore
  • an underscore sorts before a lower-case letter
Except for the end-of-name rule, this is the same as the character sort order:
sort(['a':'z', '0':'9', '_', 'A':'Z'])
ans = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
syms T T1 TU T_ Tu
Tu*T_*TU*T1*T
ans = 
But even with this in mind, it is still quite impossible to figure out the right order of matrix multiplication from the result, especially when operating on large size matrices :)
Anyways, good to know how it sorts symbols. Thanks for sharing.
Note that as of R2021a, defining symbolic matrix became possible. R2021a was released a couple of weeks after the question was asked, so as of the time the question was asked, no released version handled the task, only prerelease
That is good to know, though I do not need to use this function for now. Thank you Walter for the update!

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by