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

조회 수: 6 (최근 30일)
Ravi Gondhalekar
Ravi Gondhalekar 2019년 7월 9일
답변: Cal Brooks 2019년 8월 29일
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));
end
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?

답변 (2개)

Cal Brooks
Cal Brooks 2019년 8월 29일
The best way to force a cell array to be homogeneous is to index into the array with a non-constant value. As noted in the documentation: “if you index into this cell array with an index whose value is determined at run time, the code generator classifies it as a homogeneous cell array.“
The following code is one way to acheive that:
if ~isempty(c)
c{coder.ignoreConst(1)};
end

Sean de Wolski
Sean de Wolski 2019년 7월 9일
Why bother using a cell array v. a 3d array of size [dims1 dims2 N]?
  댓글 수: 1
Ravi Gondhalekar
Ravi Gondhalekar 2019년 7월 11일
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.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by