Effacer les filtres
Effacer les filtres

dynamic allocation of arrays

6 vues (au cours des 30 derniers jours)
Eugenio Grabovic
Eugenio Grabovic le 6 Août 2020
Réponse apportée : KSSV le 6 Août 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.

Réponse acceptée

KSSV
KSSV le 6 Août 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

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by