Dynamic Array

버전 1.0.0.2 (8.56 KB) 작성자: Serhan Yilmaz
Efficiently handle array resizing on the fly by dynamic allocation. Great for applications with unknown array sizes
다운로드 수: 16
업데이트 날짜: 2023/5/17

라이선스 보기

DynamicArray is a versatile function that enables dynamic memory allocation for arrays with unknown sizes. It offers a convenient and efficient solution for scenarios where preallocating the array size is not feasible. By utilizing a factor growth strategy, the class dynamically expands the array by a certain percentage factor (default 2) whenever necessary, reducing the frequency of memory reallocation. This approach, with a theoretical cost of N·log(N) that is practically linear, strikes a good balance between performance and memory utilization.
The DynamicArray class provides flexibility by supporting two array types: numeric and cell arrays. Numeric arrays are initialized with zeros, while cell arrays are initialized with empty cells. The initial capacity of the array can be specified during instantiation, with a default value of 100 if not provided.
The class offers seamless methods for adding and inserting elements into the array. When the current capacity is insufficient, the array is automatically enlarged according to the factor growth strategy. The 'add' and 'insert' methods accept vector inputs and handle both numeric and cell arrays.
In the end, the 'finalize' method returns a trimmed version of the array, excluding any unused capacity. This feature is particularly useful when the final array needs to be passed to other parts of the program, ensuring that only relevant elements are included.
In summary, the DynamicArray class seamlessly combines the benefits of dynamic memory allocation and on-the-fly preallocation. It can offer convenience, relative efficiency, and flexibility for managing arrays with dynamic sizes, making it a valuable asset in a broad range of applications.
Example Usage:
myArray = DynamicArray(100);
% Generate and add 1000 random numbers to the array
for i = 1:10000
randomNumber = randi([1, 100]);
myArray.add(randomNumber);
end
myArray = myArray.finalize();
Example for cell arrays:
myCellArray = DynamicArray(100, 'cell');
% Generate and add 1000 random strings to the array
availableFruits = {'apple', 'banana', 'orange', 'grape'};
for i = 1:10000
randomNumber = randi([1, 4]);
str = availableFruits{randomNumber};
myCellArray.add(str);
end
myCellArray = myCellArray.finalize();

인용 양식

Serhan Yilmaz (2026). Dynamic Array (https://kr.mathworks.com/matlabcentral/fileexchange/129704-dynamic-array), MATLAB Central File Exchange. 검색 날짜: .

MATLAB 릴리스 호환 정보
개발 환경: R2022b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux

DynamicArray

버전 게시됨 릴리스 정보
1.0.0.2

Added demo and benchmarking

1.0.0.1

Added examples

1.0.0