dynamic allocation of arrays

조회 수: 3 (최근 30일)
Eugenio Grabovic
Eugenio Grabovic 2020년 8월 6일
답변: KSSV 2020년 8월 6일
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.

채택된 답변

KSSV
KSSV 2020년 8월 6일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by