Clear Filters
Clear Filters

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

2 views (last 30 days)
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')
 

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Feb 2024
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.
 

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!