dynamic allocation of arrays

1 Ansicht (letzte 30 Tage)
Eugenio Grabovic
Eugenio Grabovic am 6 Aug. 2020
Beantwortet: KSSV am 6 Aug. 2020
Hi i have the following code where in a loop i reallocate the array "resultPolygon". Consider it as a pseudocode, which is simple to understand cause i would need to attach too many data and functions to let u run the real code.
shaper = 3xm array;
resultPolygon = 2xn array;
for i = 1:720
newShaper = homogeneousTransformMatrix(phi(i))*shaper; % moving the subtracting object
resultPolygon = boolSub(resultPolygon, newShaper(1:2,:));
end
Basically im applying subsequent boolean operations on a polygon from which is subtracted the moving "shaper" polygon. "resultPolygon" is just a 2xn matrix of doubles. This matrix data grows at each iteration and the result is reallocated into the variable. I saw, here on the forums, a way to deal with dynamic arrays by preallocating a "big enough" array before the loop and trimming the unnecessary data afterwards. My doubt is how to apply this method for this specific case, since here, at each iteration i don't know by how much the "resultPolygon" data grows, and, in general, any of the already allocated elements can change aswell.

Akzeptierte Antwort

KSSV
KSSV am 6 Aug. 2020
You can initiate it to a cell.
resultPolygon = cell(720,1);
for i = 1:720
newShaper = homogeneousTransformMatrix(phi(i))*shaper; % moving the subtracting object
resultPolygon{i} = boolSub(resultPolygon, newShaper(1:2,:));
end

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by