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

4 views (last 30 days)
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
>>

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 19 Aug 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
ご参考:

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!