MATLAB 関数を MATLAB Coder で MEX 化するとサイズや次元​の不一致が起こるのは​なぜですか?

4 vues (au cours des 30 derniers jours)
MathWorks Support Team
MathWorks Support Team le 19 Août 2020
MATLAB 環境上では問題なく動作する MATLAB 関数の .m ファイルを、MATLAB Coder で MEX ファイルに変換して実行すると、入力引数は同じにもかかわらず、サイズや次元が一致しないといったエラーとなります。
(1) 以下のような MATLAB 関数を作成します。
function example()
x = [1 2 3];
y = [1 2 3; 4 5 6; 7 8 9];
z = x+y;
(2) MATLAB 環境上では問題なく実行することができます。
>> example
(3) MATLAB Coder で MEX 化しようとすると以下のようなエラーとなります。
>> codegen example
??? サイズの不一致 (サイズ [1 x 3] ~= サイズ [3 x 3])。
エラー ==> example 行: 4 列: 5
コードを生成できません: エラー レポートの表示
エラー: codegen
>>

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 19 Août 2020
原因は、MATLAB環境で実行する場合、サイズの違いは暗黙的な拡張により吸収されますのでエラーとならずに実行できますが、MATLAB Coderでは暗黙的な拡張をサポートしていないため、エラーとなります。
このような場合はbsxfunを使用します。
(1)以下のように修正します。
function example()
x = [1 2 3];
y = [1 2 3; 4 5 6; 7 8 9];
%z = x+y;
z = bsxfun(@plus,y,x);
(2) 以下のようにMEX化します。
>> codegen example
ご参考:

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB Coder dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!