Why do I get the error "Computed maximum size is not bounded" when using colon operator with unbounded arguments in MATLAB Coder?

5 次查看(过去 30 天)
The following code snippet, which initializes an array 'rowIndex' with the colon operator:
assert(all(size(values) == [5,5]),'size mismatch');
[nx,ny] = size(values);
for this_row = 1:nx
    start_row = this_row -1;
    end_row = this_row + 1;
    if start_row == 0 % row 1
        start_row = 1;
    elseif end_row > nx % row 5
        end_row = nx;
    end
    rowIndex = start_row:end_row;
end
causes MATLAB Coder to throw the error:
??? Computed maximum size is not bounded.
Static memory allocation requires all sizes to be bounded.
However, I do not receive an error when using 'linspace' as shown below:
assert(all(size(values) == [5,5]),'size mismatch');
[nx,ny] = size(values);
for this_row = 1:nx
start_row = this_row -1;
end_row = this_row + 1;
if start_row == 0 % row 1
start_row = 1;
elseif end_row > nx % row 5
end_row = nx;
end
numelRow = end_row-start_row + 1;
assert(numelRow <= 5);
rowIndex = linspace(start_row,end_row,numelRow);
end
Why does the error occur? (Assume that the coder.config setting 'DynamicMemoryAllocation' is set to 'Off')
 

采纳的回答

MathWorks Support Team
The code with the colon operator fails because MATLAB needs the arguments passed to the colon operator to be bounded, since it uses these arguments to calculate the size of 'rowIndex'. Here ‘start_row’ and ‘end_row’ are not explicitly bounded by ‘assert’ statements.
In contrast, the code using 'linspace' passes the assert-bounded argument ‘numelRow’ to ‘linspace’. Because this argument defines the size of ‘rowIndex’, MATLAB knows explicitly how much space to reserve for this array in memory.
In general, favor using ‘linspace’ over the colon operator when the maximum size of the array needs to be known beforehand.
 

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by