How to stop matlabFunction() from inherently using reshape()?

2 Ansichten (letzte 30 Tage)
Hi All,
I'm using matlabFunction() to convert a symbolic expression into a function. The expression is a matrix, however it is NOT a symmatrix. The matrix is a function of many parameters and is called often, so it's seemed straightforward to derive the matrix elements symbolically and then turn it into a function that can be called repeatedly.
The problem is: the function that is produced wraps the matrix with a reshape("mymatrix",[dim1,dim2]). Is there a way for the matlabFunction() to not do this? For reference, This issue seems to be close to the problem I'm having.
The main reason why this is a problem is because the elements of the matrix are actually vectors of known size, but it's easiest to keep them as symbolic parameters until i need to actually substitue in the vectors.
For example:
syms Z B C real
A = [Z,B;C,Z];
AFun = matlabFunction(A,'Vars',[Z,B,C]); % This will effectively create reshape([Z,B,C,Z],[2,2])
AFun(ones(1,2),ones(1,2),ones(1,2)) % However Z,B,C are actually row vectors.
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.

Error in symengine
If AFun did not have the reshape() and simply was [Z,B;C,Z] then there'd be no problem. The resulting matrix would be (2x4). I suppose I could copy/paste the entire sym matrix output and reform it into the proper function. The comment in the linked post said this was a known issue, so really part of the question is if this issue I'm presenting is based on the same issue.
Thanks!
CP
  1 Kommentar
Caleb Peck
Caleb Peck am 11 Okt. 2022
I realize that the code in this question was run in 2022b, my current version is 2021b.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Okt. 2022
No, there is no hope of that in any existing version of MATLAB. If your expression is not scalar then matlabFunction will always generate code that uses reshape() and an assumption of a fixed size. Successful vectorization is only possible for scalar input expressions.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by