MATLAB Answers

Is there an elegant way to create dynamic array in MATLAB?

390 views (last 30 days)
liangjian
liangjian on 11 Nov 2011
Answered: Hooman Sedghamiz on 6 Mar 2018
Is there an elegant way to create dynamic array in MATLAB?
For instance, my program has a loop, as program iterates over the loop, some numeric values will be generated and should be stored in an array. However, I cannot know the exact count of these generated values because their generation depends on an if-else logic. How to pre-allocate the size of this array used for storing these values.

Accepted Answer

Jan
Jan on 11 Nov 2011
It is much more efficient to allocate too much memory (as long as it fits into the available RAM) than letting an array grow. Usually you can determin a maximum size and crop the array afterwards:
n = 1000;
A = zeros(n, 6);
i = 0;
for i = 1:n
if rand < 0.5
i = i + 1;
A(i, :) = rand(1, 6);
end
end
A = A(1:i, :);
But sometimes estimating an upper limit for the size is impossible. Then you can pre-allocate in chunks. This has actually the same problem as an omitted pre-allocation, but the with a very much smaller effect:
n = 1000;
A = zeros(0, 6);
i = 0;
imax = 1000;
for i = 1:n
if rand < 0.5
i = i + 1;
if i > imax % Allocate in chunks of 1000 rows
imax = imax + 1000; % EDITED
A(imax, :) = 0;
end
A(i, :) = rand(1, 6);
end
end
A = A(1:i, :);
  5 Comments
Hooman Sedghamiz
Hooman Sedghamiz on 6 Mar 2018
As others correctly noted, it is not a good practice to use a not pre-allocated array as it highly reduces your running speed. There are two ways to do it;
Solution 1: In fact it is possible to have dynamic structures in Matlab environment too. However, it is not a native Matlab structure. Recently, I had to write a graph traversal script in Matlab that required a dynamic stack. To do so, you can simply use a Stack from java libraries for example. It is quite powerful and you can handle almost all different types of data! You can simply borrow this from java within matlab by;
% imports stack utility from java in matlab
import java.util.*;
% then use following commands to deal with stack
A = Stack();
A.push(i); % inserts i on top of stack A
A.pop(); %pops out an element
Solution 2: As Jan's said , preallocate an overestimated large array and then trim it after your operations.

Sign in to comment.

More Answers (3)

Jonathan
Jonathan on 11 Nov 2011
liangjian,
You can preallocate based on the number of iterations. Try something like this.
numIter = 100;
array = nan(numIter, 3);
for i = 1:numIter
if randi(2) == 1
newValues = [i 10*i 100*i];
array(i,:) = newValues;
end
end
array = array(~isnan(array(:,1)),:);

Image Analyst
Image Analyst on 11 Nov 2011
Sure, just don't do anything and that's the way MATLAB works. Just start using the variable. However it's better to preallocate, like Jan and Jonathan told you, even if it's only an estimate. If you preallocate with zeros() or something, and then you can adjust the size automatically later just by assigning to it. For example:
a = zeros(100,100); % Preallocate a guess
a(200, 300) = 4; % Automatically adjusts size.
A clever trick by Loren Shore to preallocate is to just assign the lower right element of the array, for example:
b(480,640) = 0;
Note though that only the lower right element takes on the value you set, not the whole array. The rest of the array will be zero. For example:
b(4, 2) = 40
b =
0 0
0 0
0 0
0 40

Hooman Sedghamiz
Hooman Sedghamiz on 6 Mar 2018
As others correctly noted, it is not a good practice to use a not pre-allocated array as it highly reduces your running speed. There are two ways to do it;
Solution 1: In fact it is possible to have dynamic structures in Matlab environment too. However, it is not a native Matlab structure. Recently, I had to write a graph traversal script in Matlab that required a dynamic stack. To do so, you can simply use a Stack from java libraries for example. It is quite powerful and you can handle almost all different types of data! You can simply borrow this from java within matlab by;
% imports stack utility from java in matlab
import java.util.*;
% then use following commands to deal with stack
A = Stack();
A.push(i); % inserts i on top of stack A
A.pop(); %pops out an element
Solution 2: As Jan's said , preallocate an overestimated large array and then trim it after your operations.

Community Treasure Hunt

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

Start Hunting!