How can I achieve Matrix-Vector element-wise operations in Simulink Matlab Function Block?
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Naming Conventions in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!