【问题标题】:Error in Matlab Coder: Index exceeds array dimensionsMatlab Coder 中的错误:索引超出数组维度
【发布时间】:2021-06-30 12:14:57
【问题描述】:

我正在尝试使用 MATLAB Coder 将 .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/100P_1s.*randn(size(P_1s) )/2)/9;zeros(30,9)].

  1. 将 x 声明为可变大小是否正确?如果是,那么“索引超出数组维度错误”的解决方法是什么

请告诉我,我缺少什么将其转换为 C++ 代码

【问题讨论】:

    标签: arrays indexing matlab-coder


    【解决方案1】:

    MATLAB Coder 不支持您在此处使用的 2 个内容:implicit expansiongrowing arrays by assigning 在维度末尾之后。

    对于隐式扩展,您可以使用:

    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)];
    

    【讨论】:

    • 嗨@Ryan,但是如果我通过使用连接来增长我的数组'x',x 的新大小将是(9 x (tmax+11))。在我的代码P_1s=[ones(12,9)*p(1,2)/9;(P_1s+var3/100*P_1s.*randn(size(P_1s))/2)/9;zeros(30,9)].*x/100; 的最后但第二行中,我正在执行元素明智的乘法。要进行元素乘法,尺寸应该匹配。随着 x 的大小现在增加,最终会引发错误。
    • 我想了这么多,这就是为什么我添加了评论来检查我的上限的列数。看起来tmax 的列数是错误的。在发生错误的行处设置断点并比较传递给.* 的大小。这将告诉您在x=[x, zeros(9,tmax)] 中使用什么代替tmax。它可能需要 -10 或 -11 吗?
    • 是的,谢谢@Ryan。知道了! tmax 应为 1279,即 -11。对于我们通过连接初始化的矩阵,如果有结构怎么办?也许我会写一个示例程序并尝试找到解决方案。无论如何,再次感谢瑞恩。
    • Ryan,我使用结构 function []=fu() structure = struct('a', 5,'b', ones(5,6)); % coder.varsize('structure') for i=1:10 structure(i).a=i+2; end temp =[1,structure(3).a; 2,structure(7).a]; end 编写了小 sn-p 但是,我收到错误 索引表达式超出范围。尝试访问元素 3。有效范围是 1-1。 通常我们会使用正确的结构,所以出于好奇,我编写了这个函数。如果没有意义,请忽略。
    • 结构数组的想法完全相同。预先分配数组或通过连接增长:s = repmat(struct(...), m, n) 或使用 s = [s, struct(...)] 在循环内增长。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多