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

### liangjian (view profile)

on 11 Nov 2011
Latest activity Answered by Hooman Sedghamiz

on 6 Mar 2018

### Jan (view profile)

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.

### Jan (view profile)

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, :);

Jonathan

### Jonathan (view profile)

on 11 Nov 2011
@Jan, I should have been more careful. Your for loop variable should be difference than i!
Jan

### Jan (view profile)

on 13 Nov 2011
@Jonathan: I know, that avoiding i and j is recommended in the Matlab documentation. And in the same documentation I find a lot of examples, which do use i as a counter in a FOR loop. I personally never hat a problem with using i in simple loops, because I avoid terms like 2*i and use 2i instead.
Hooman Sedghamiz

### Hooman Sedghamiz (view profile)

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.

### Jonathan (view profile)

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 (view profile)

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 (view profile)

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.