How can I achieve Matrix-Vector element-wise operations in Simulink Matlab Function Block?

TL;dr matlab seems to support elementwise operations when one of the elements is a vector of the appropriate length (e.g. [4x3] .^ [4x1]). Simulink seems to require that they are both matrices of identical sizes (e.g. [4x3] .^ [4x3]). Can I achieve the matlab functionality in a simulink matlab function block? If not, why?
Here is the MWE; a matlab function block that spits the results into a display block:
MWE.PNG
Here is the code for the matlab function block. It's a one-line function, and I have three different versions.
This one doesn't work.
function ans = fcn()
ans = ones(4,3) .^ ones(4,1);
It produces an error about matrix dimensions needing to agree (see below or the full error). It suggests specifying the output size, which I tried (4x3) and that did not resolve the issue. Additionally, this line of code runs perfectly fine in matlab:
ones(4,3) .^ ones(4,1)
ans =
1 1 1
1 1 1
1 1 1
1 1 1
This one does work because the dimensions match exactly:
function ans = fcn()
ans = ones(4,3) .^ ones(4,3);
This is my workaround to use the original dimensions:
function ans = fcn()
ans = ones(4,3) .^ repmat(ones(4,1),1,3);
I know it's only one extra function call, but I would love to avoid excessive repmat() calls throughout my entire project. Is there a way to achieve the elementwise operation with a matrix and a vector in a simulink matlab function? If not, is there some rational why simulink doesn't support this functionality?
Thank you!
Here's the full error message:
Matrix dimensions must agree. Function 'MATLAB Function1' (#104.49.71), line 4, column 7: "ones(4,3) .^ ones(4,1)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'MWE/MATLAB Function1'
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'MWE/MATLAB Function1' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.
Component:MATLAB Function | Category:Coder error
Simulink cannot determine sizes and/or types of the outputs for block 'MWE/MATLAB Function1' due to errors in the block body, or limitations of the underlying analysis. The errors might be inaccurate. Fix the indicated errors, or explicitly specify sizes and/or types for all block outputs.

 Accepted Answer

I'm not sure why the MATLAB Function block can not handle the implicit scalar expansion, but I just tried using an Interpreted MATLAB function block which calls a function which executes ones(4,3).^ones(4,1) and it works without any errors. So if your application allows you to use Interpreted MATLAB function blocks that may be a work around. I'm not sure what limitations/performance tradeoffs there are for those two kinds of blocks which seem to do somewhat similar jobs.

5 Comments

Thank you, but I'm strictly interested in using a matlab function for performance reasons. My workaround using matrep() is going to be sufficient to continue using the matlab function, but I would really like to utilize implicit scalr expansion if possible. This function is a bottleneck for a larger simulation, so it needs to be fast. Here's a link about the tradeoffs between a matlab function and an interpreted matlab function:
Using bsxfun seems to work inside of a MATLAB function block. I don't know if the performance hit for repmat is better or worse than using bsxfun, but it is at least another possibility
y = bsxfun(@power,ones(4,3),ones(4,1))
Thanks! This is also a great workaround, and a bit more readable. I still don't really understand what under-the-hood limitations prevent simulink from applying implicit expansion (and if there is a way to directly circumvent those limitations), but I guess I may never know. Either way, my code will work at least.
Glad this at least helps a little. You could report this as a bug to MATLAB. One good thing about reporting the bug, is that if it is eventually fixed they send you a notification.

Sign in to comment.

More Answers (0)

Categories

Find more on Reporting and Database Access in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 12 Feb 2020

Commented:

on 18 Feb 2020

Community Treasure Hunt

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

Start Hunting!