MATLAB 関数を MATLAB Coder で MEX 化するとサイズや次元の不一致が起こるのはなぜですか?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 19 Aug. 2020
Beantwortet: MathWorks Support Team
am 19 Aug. 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
>>
Akzeptierte Antwort
MathWorks Support Team
am 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
ご参考:
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu MATLAB Coder finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!