I was playing around with tables
as a replacement for regular numerical arrays for various reasons, when I came across the following challenge: how to (pre-)allocate a table with non-scalar variables?
Given a loop like so:
function A = myfun(...)
N = large number
A = zeros(N,4);
for i = 1:N
do stuff
A(i,:) = [scalar, vector];
end
I want to instead return a table with named variables.
I could simply rewrite it to say:
function T = myfun2(...)
N = large number
A = zeros(N,4);
for i = 1:N
do stuff
A(i,:) = [scalar, vector];
end
T = table(A(:,1), A(:,2:end),'VariableNames',{'scalar','vector'});
which obviously yields a table with the format:
T =
N×2 table
scalar vector
______ ___________
0 0 0 0
0 0 0 0
0 0 0 0
... ...
Now, if I instead wanted to pre-allocate the output table and update it for every iteration I would try something along the lines of:
function T = myfun3(...)
N = large number
T = table('Size',[N,2],...
'VariableTypes',{'double','double'},...
'VariableNames',{'scalar', 'vector'});
for i = 1:N
do stuff
T(i,:) = {scalar, vector};
end
The problem with myfun3
is that the format of T is:
T =
N×2 table
scalar vector
______ ______
0 0
0 0
0 0
So clearly the variable 'vector' is now scalar instead of an array/vector. Reading from the table
documentation it does not seem like the 'size' type pre-allocation can take in array sizes?
Q1: How does one go about pre-allocating a table
with non-scalar variables?
Q2: If A in myfun2
is large, is the overhead bad or is this an acceptable solution?
I have concerns that the extra overhead of indexing into/out-of a table are exceedingly large compared to a numerical array that it will adversely effect performance code.
======= EDIT =======
I contacted MathWorks and they confirmed that as of MATLAB R2019b there is no way of achieving Q1 with the size
parameter.