Is there an elegant way to create dynamic array in MATLAB?
76 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Jan
2011-11-11
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, :);
6 个评论
Hooman Sedghamiz
2018-3-6
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.
Konner Brickey
2023-2-23
There is one additional problem with the above code, and maybe this is new since I am 10 years late. When The original code runs
n = 10;
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, :);
the answer is
A
Which is not cropped to remove the zeros. I think the problem is that while i is set to 0 first, the for loop resets the value of i, negatting the counter. If the indexing variable for the loop is changed to j
n = 10;
A = zeros(n, 6);
i = 0;
for j = 1:n
if rand < 0.5
i = i + 1;
A(i, :) = rand(1, 6);
end
end
B = A(1:i, :);
we get
B
the actual cropped data.
Let me know if I am missing something :)
更多回答(3 个)
Jonathan
2011-11-11
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)),:);
0 个评论
Image Analyst
2011-11-11
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
0 个评论
Hooman Sedghamiz
2018-3-6
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!