[英]Error in Matlab Coder: Index exceeds array dimensions
我正在嘗試使用 MATLAB 編碼器將.m 腳本轉換為 C++。
function P=r_p(1,var1,var3)
p=[[3,7]
[10,15]
[6,19]
[21,19]
[43,11]
[969,2]
[113,9]
[43,59]
[21,15]
[6,15]
[10,18]
[3,15]];
tmax=sum(p(:,1))+41;
coder.varsize('x');
x=ones(9,11).*[0:10:100]; % getting error in this line: [9x11]~=[1x11]. Since size of x is varying in for loop, so i should tell coder that it is variable size, So I used Varsize
for t=11:tmax
a1=(rand-0.5)*1;
a9=(rand-0.5)*1.25;
a2=(rand-0.5)*1.5;
a8=(rand-0.5)*1.75;
a3=(rand-0.5)*2.0;
a7=(rand-0.5)*2.25;
a4=(rand-0.5)*2.5;
a6=(rand-0.5)*2.75;
a5=(rand-0.5)*3;
x(1,t+1)=x(1,t)+a1;
if x(1,t+1)<(100-var1) || x(1,t+1)>(100+var1) % loop 1: x(1,11)+a1 value is is writing to x(1,12) So coder gives error "Index exceeds array dimensions. Index value 12 exceeds valid range [1-11] of array x".
x(1,t+1)=x(1,t); % In matlab it works fine, but coder throws error.
end
end
我的問題是假設循環 1,x(1,12)= x(1,11)+a1 在 matlab 中,此分配工作正常,但轉換時拋出錯誤“索引超出數組尺寸。索引值 12 超出有效范圍 [數組 x 的 1-11]" 因為我將 x 聲明為可變大小編碼器應該將 x(1,11)+a1 值分配給 x(1,12) 但它沒有這樣做,而是拋出錯誤。 為什么?
由於 t 為 1289 循環,如果我為 x 指定邊界,例如coder.varsize('x',[1290,1290],[0,0])
則 Coder 在代碼的其他部分給出錯誤,即尺寸不匹配. 當然應該是因為 x 的維度與 [ones(12,9) p(1,2)/9;(P_1s+var3/100 P_1s.*randn(size(P_1s))/2)/9; 不匹配零(30,9)]。
請告訴我,我缺少什么將其轉換為 C++ 代碼
MATLAB 編碼器不支持您在此處使用的兩件事: 隱式擴展和通過分配維度末尾的 arrays 來增長。
對於隱式擴展,您可以使用:
x=bsxfun(@times,ones(9,11),[0:10:100]);
在 MATLAB 中分配數組末尾之后將增大數組。 這是 Coder 中的一個錯誤。 有兩種方法可以克服這個問題:
x = [x, newColumn]
在此示例中,您知道tmax
,因此我建議您更改x
的分配以預先設置正確的列數:
% Current initial value
x=bsxfun(@times,ones(9,11),[0:10:100]);
% Extra columns - please check my upper bound value
x=[x, zeros(9,tmax)];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.