mex - matlab coder: cannot transpose a matrix that is incorrectly deemed to be 3-D when it should always be 2-D -
i have function (with code given below) wish convert mex file. trying utilize matlab coder 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 calls function = 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 %% phone call function potential_mat = func_test(i,coordfile_mat,radii_mat,stat_mat,... ind_cr,len_stat_atoms,num_stat_atoms,num_coord_atoms,... counter,num_divs,dof); end %% function code function potential_mat = func_test(i,coordfile_mat,radii_mat,stat_mat,... ind_cr,len_stat_atoms,num_stat_atoms,num_coord_atoms,... counter,num_divs,dof); potential_mat = zeros(num_coord_atoms,num_divs^dof); j = 1:size(coordfile_mat,3) %% compute distances = zeros(3,len_stat_atoms(i)); = coordfile_mat(:,1:len_stat_atoms(i),j); b = zeros(3,len_stat_atoms(i),num_stat_atoms(i)); b = repmat(a,[1 1 num_stat_atoms(i)]); c = zeros(1,len_stat_atoms(i),num_stat_atoms(i)); c = sqrt(sum((b - stat_mat).^2,1)); d = zeros(len_stat_atoms(i),num_stat_atoms(i)); d = shiftdim(c,1); distances = zeros(num_stat_atoms(i),len_stat_atoms(i)); distances = d'; %% compute clashes , 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 on 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 end
the problem occurs @ sec line below, throws error because cannot transpose nd matrix.
d = shiftdim(c,1); distances = d';
matlab coder identifies c
of size 1 x :? x :?
, correct. d = shiftdim(c,1)
line should yield 2d matrix of size :? x :?
, transpose. however, fails correctly shift dimensions of d
, , gives size of :? x :? x :?
. causes transpose error. how can resolve problem? furthermore, own edification, why matlab coder unable correctly assign dimensions of variables, a
, b
, c
, d
, , distances
?
the docs describe incompatibility matlab in determining size of variable-size n-d arrays (d
created in loop, seems variable size):
for variable-size n-d arrays, size function can homecoming different result in generated code in matlab. in generated code, size(a) returns fixed-length output because it not drop trailing singleton dimensions of variable-size n-d arrays. contrast, size(a) in matlab returns variable-length output because drops trailing singleton dimensions.
for example, if shape of array a
:?x:?x:?
, size(a,3)==1
, size(a)
returns:
the issue addressed in above documentation pertains utilize of size
function. however, in case, want able transpose. workaround utilize permute
, since transpose general case of permute
:
% after shiftdim: distances = permute(d,[2 1 3]) % same d' 2d array, handles 3d
you cutting out shiftdim
entirely:
distances = permute(c,[2 3 1])
also, don't have much experience coder, require initializations zeros
? seem unnecessary.
mex matlab-coder
No comments:
Post a Comment