MATLAB Answers


How can I define a homogeneous cell array with elements of a fixed size?

Asked by Ravi Gondhalekar on 9 Jul 2019
Latest activity Commented on by Ravi Gondhalekar on 11 Jul 2019
I have a Matlab code that is to be auto-coded to C for use in mex-functions and standalone libraries. The code generation must use the configuration options that disallow dynamic memory allocation (DynamicMemoryAllocation='Off') and disallow variable-size arrays (EnableVariableSizing=false). I wish to define a cell array X that has a number of elements (e.g., N=5) that is known at the time of auto-coding. Each element X{i} of the cell array X is a matrix of doubles with dimensions that are fixed over the execution, but may be distinct from the dimension of other elements of the cell array. The dimensions of each element are known at the time of auto-coding, but are stored in vectors, rather than being hard-coded.
A cartoon of what I want to achieve is below:
N = 5;
X = cell(N,1);
[ dims1, dims2] = getDimensions(N); % Get N-length vectors containing the dimensions
for i = 1:N
X{i} = zeros( dims1(i), dims2(i));
Depending on subtle aspects of the rest of the code the coder sometimes defines the cell arrays as homogeneous, but at other times defines them as heterogeneous. For reasons of efficiency and indexing (details omitted) I want the cell array X to be homogeneous. I can force this via
coder.varsize( 'X', [ N 1 ], [ 0 0 ]);
but that requires the option to allow variable-size arrays (EnableVariableSizing=true), even though I employ the "varsize" directive to state that the sizes are not variable. Without using the "varsize" directive I only sometimes end up with a homogeneous cell array, and sometimes with a heterogeneous one.
Q: How can I communicate to the coder that I wish to define a homogeneous cell array with elements of a fixed size?


Sign in to comment.

1 Answer

Answer by Sean de Wolski
on 9 Jul 2019

Why bother using a cell array v. a 3d array of size [dims1 dims2 N]?

  1 Comment

Sean, thank you for responding. Each element of the cell array may have different dimensions, i.e., possibly size(X{i}) ~= size(X{k}) for i ~= k. I edited the question and added a comment to explain that dims1 and dims2 are vectors of length N. The dimensions are possibly significantly different, therefore I consider it undesirable to use a 3D array of the maximum dimensions.

Sign in to comment.