Why do I get the error "Computed maximum size is not bounded" when using colon operator with unbounded arguments in MATLAB Coder?
5 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2024-2-23
回答: MathWorks Support Team
2024-2-29
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
2024-2-29
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!