How to stop matlabFunction() from inherently using reshape()?
1 次查看(过去 30 天)
显示 更早的评论
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.
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
采纳的回答
Walter Roberson
2022-10-11
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!