简体   繁体   English

Matlab 编码器中的错误:索引超出数组尺寸

[英]Error in Matlab Coder: Index exceeds array dimensions

I am trying to convert.m script to C++ using MATLAB Coder.我正在尝试使用 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

My question is Let say loop 1, x(1,12)= x(1,11)+a1 In matlab this assignment works fine, but when converting it is throwing error " Index exceeds array dimensions. Index value 12 exceeds valid range [1-11] of array x" As I declared x as variable size coder should assign x(1,11)+a1 value to x(1,12) but it is not doing, instead throwing error.我的问题是假设循环 1,x(1,12)= x(1,11)+a1 在 matlab 中,此分配工作正常,但转换时抛出错误“索引超出数组尺寸。索引值 12 超出有效范围 [数组 x 的 1-11]" 因为我将 x 声明为可变大小编码器应该将 x(1,11)+a1 值分配给 x(1,12) 但它没有这样做,而是抛出错误。 Why?为什么?

Since t is looping for 1289, if I specify bounds for x like coder.varsize('x',[1290,1290],[0,0]) then Coder gives error in other part of the code ie dimensions doesn't match.由于 t 为 1289 循环,如果我为 x 指定边界,例如coder.varsize('x',[1290,1290],[0,0])则 Coder 在代码的其他部分给出错误,即尺寸不匹配. Ofcourse it should because dimension of x doesn't match with [ones(12,9) p(1,2)/9;(P_1s+var3/100 P_1s.*randn(size(P_1s))/2)/9;zeros(30,9)].当然应该是因为 x 的维度与 [ones(12,9) p(1,2)/9;(P_1s+var3/100 P_1s.*randn(size(P_1s))/2)/9; 不匹配零(30,9)]。

  1. is declaring x as variable size is correct step or not?将 x 声明为可变大小是否正确? if yes then what should be the work around for "index exceeds array dimensions error"如果是,那么“索引超出数组维度错误”的解决方法是什么

Please Let me know, what am I missing to convert it to C++ code请告诉我,我缺少什么将其转换为 C++ 代码

MATLAB Coder doesn't support 2 things you're using here: implicit expansion and growing arrays by assigning past the end of a dimension. MATLAB 编码器不支持您在此处使用的两件事: 隐式扩展通过分配维度末尾的 arrays 来增长

For implicit expansion, you can use:对于隐式扩展,您可以使用:

x=bsxfun(@times,ones(9,11),[0:10:100]);

Assigning past the end of an array in MATLAB will grow the array.在 MATLAB 中分配数组末尾之后将增大数组。 That's an error in Coder.这是 Coder 中的一个错误。 There are 2 ways to overcome this:有两种方法可以克服这个问题:

  • Allocate your array to have the right number of elements up front分配您的数组以预先设置正确数量的元素
  • Use concatenation to grow an array: x = [x, newColumn]使用串联来增长数组: x = [x, newColumn]

In this example, you know tmax so I'd suggest just changing the allocation of x to have the right number of columns up front:在此示例中,您知道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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM