Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
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')
 

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 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.
 

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by