MATLAB Coder: Help with input matrices that are not of constant size

4 views (last 30 days)
I have a function (with the code given below) that I wish to convert into a MEX file. I am trying to use matlab coder to do so.
%%Inputs
dof = 3;
num_divs = 72;
ind_cr =
0 0
1 1
1 2
2 2
1 3
2 3
3 3
ind_switch =
1
1
1
0
1
0
0
len_stat_atoms =
1
72
5184
72
373248
5184
72
num_stat_atoms =
108
111
111
3
111
3
3
coordFile =
[3x1 double]
[3x1 double]
[3x72x3 double]
[3x5184x3 double]
[3x373248x4 double]
radii_cell =
[108x1x3 double]
[111x72x3 double]
[111x5184x3 double]
[ 3x72x3 double]
[111x373248x4 double]
[ 3x5184x4 double]
[ 3x72x4 double]
stat_cell =
[3x1x108 double]
[3x72x111 double]
[3x5184x111 double]
[3x72x3 double]
[3x373248x111 double]
[3x5184x3 double]
[3x72x3 double]
%%Code that calls function
for i = 1:(dof*(dof+1)/2+1)
%%Load matrices
radii_mat = radii_cell{i,1};
stat_mat = stat_cell{i,1};
if ind_switch(i)
coordFile_mat = coordFile{ind_cr(i,2)+2};
end
%%Call the function
function [output] = foo(appropriate inputs)
end
%%Function code
potential_mat = zeros(num_coord_atoms,num_divs^dof);
for j = 1:size(coordFile_mat,3)
%%Compute distances
temp_coordFile = coordFile_mat(:,1:len_stat_atoms(i),j);
temp_coordFile = repmat(temp_coordFile,[1 1 num_stat_atoms(i)]);
distances = shiftdim(sqrt(sum((temp_coordFile - stat_mat).^2)),1)';
%%Compute clashes and potentials
clashes = distances < radii_mat(:,:,j);
potentials = zeros(size(distances));
potentials(clashes) = (1-(distances(clashes)./radii_mat(find(clashes)+numel(clashes)*(j-1))).^6).^2;
%%Iterate over nodes
col = ind_cr(i,1); row = ind_cr(i,2);
if col == 1
ind_kron = repmat(1:size(potentials,2),[num_divs^(dof-row) 1]);
potentials = potentials(:,ind_kron(:)');
elseif row == dof
vec_repmat = [1 num_divs^(col-1)];
potentials = repmat(potentials,vec_repmat);
elseif col > 0
vec_repmat = [1 num_divs^(col-1)];
ind_kron = repmat(1:size(potentials,2),[num_divs^(dof-row) 1]);
potentials = repmat(potentials(:,ind_kron(:)'),vec_repmat);
else
potentials = repmat(sum(potentials),[1 num_divs^dof]);
end
counter = counter+1;
potential_mat(counter,:) = sum(potentials,1);
end
The main problem that arises while converting the function code into MEX occurs at this line, where it throws an error because it cannot transpose an ND matrix.
shiftdim(sqrt(sum((temp_coordFile - stat_mat).^2)),1)'
My regular matlab function works fine, and I believe the above error occurs because, when I use the 'autodefine types' GUI option, Coder imposes upper bounds and uses these max bounds to compute the sizes of the intermediary variables.
coordFile_mat double (3 x :373248 x :4)
radii_mat double (:111 x :373248 x :4)
stat_mat double (3 x :373248 x :111)
I think using these variable sizes gives rise to the transpose error. I want Coder to use the dimensions at a given instance, i.e.
temp_coordFile = radii_mat = stat_mat =
[3x1x108 double] [108x1x3 double] [3x1x108 double]
[3x72x111 double] [111x72x3 double] [3x72x111 double]
[3x5184x111 double] [111x5184x3 double] [3x5184x111 double]
[3x72x3 double] [ 3x72x3 double] [3x72x3 double]
[3x373248x111 double] [111x373248x4 double] [3x373248x111 double]
[3x5184x3 double] [ 3x5184x4 double] [3x5184x3 double]
[3x72x3 double] [ 3x72x4 double] [3x72x3 double]
and not the max bounds. Is there any way to resolve this issue? If so, can you provide clear instructions/examples using my code? Thanks!

Accepted Answer

Fred Smith
Fred Smith on 17 Nov 2014
Alejandro,
The problem is most likely not the upper bounds but rather the number of dimensions. Without the specific error message it is hard to be sure. If you know one of the dimensions has a single element but MATLAB Coder does not you can use indexing to force the number of dimensions to drop: A(:,:,1).
Hope this helps,
Fred

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!