How can I achieve Matrix-Vector element-wise operations in Simulink Matlab Function Block?
16 次查看(过去 30 天)
显示 更早的评论
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:
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.
0 个评论
采纳的回答
Jon
2020-2-12
编辑:Jon
2020-2-12
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 个评论
Jon
2020-2-14
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 General Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!