dynamic allocation of arrays

1 visualización (últimos 30 días)
Eugenio Grabovic
Eugenio Grabovic el 6 de Ag. de 2020
Respondida: KSSV el 6 de Ag. de 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.

Respuesta aceptada

KSSV
KSSV el 6 de Ag. de 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

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by