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

16 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Jon
Jon el 12 de Feb. de 2020
Editada: Jon el 12 de Feb. de 2020
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 comentarios
Jon
Jon el 14 de Feb. de 2020
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by