dynamic allocation of arrays

1 view (last 30 days)
Eugenio Grabovic
Eugenio Grabovic on 6 Aug 2020
Answered: KSSV on 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.

Accepted Answer

KSSV
KSSV on 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

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!