How to properly define input types for Matlab Coder?
조회 수: 2 (최근 30일)
이전 댓글 표시
I'm struggling to make a mex file work properly (see more here) so I decided to try and use Matlab Coder to do that hoping that would help. Using Matlab Coder I need to define the inputs, but I encountered 2 issues:
- How do I define a variable to be 3 dimensional array (the only options I see there are: scalar, vector or matrix)?
- How do I define an array with dimensions equal to a certain variable (that is also an input)?
Additional details:
The Matlab code I'm trying to convert is:
function [V,delta_V_rms,delta_V_abs,delta_V_max] = potentials_evo(L,t_max,N_eff_occ_sites_bounded,i_eff_V_bounded,j_eff_V_bounded,k_eff_V_bounded,system,over_relax_factor,V,delta_V_rms,delta_V_abs,delta_V_max)
% evolution of the potentials %
% note : for the index directions with periodic boundary conditions: index=mod(index-1,L)+1 . for index=index+1 it is mod(index,L)+1 , and for index=index-1 it is mod(index-2,L)+1 %
for t=1:t_max
for q=1:N_eff_occ_sites_bounded % this is set instead of running i=2:(L-1), j=1:L , k=1:L and ending up going over sites that are 0 in our effective system %
i=i_eff_V_bounded(q);
j=j_eff_V_bounded(q);
k=k_eff_V_bounded(q);
V0=V(i,j,k);
V1=( V(i+1,j,k)+V(i-1,j,k)+V(i,mod(j,L)+1,k)+V(i,mod(j-2,L)+1,k)+V(i,j,mod(k,L)+1)+V(i,j,mod(k-2,L)+1) )/( system(i+1,j,k)+system(i-1,j,k)+system(i,mod(j,L)+1,k)+system(i,mod(j-2,L)+1,k)+system(i,j,mod(k,L)+1)+system(i,j,mod(k-2,L)+1) ); % evolving the potential as the average of its occupied neighbors %
V(i,j,k)=V0+(V1-V0)*over_relax_factor; % evolving the potentials in time with the over relaxation factor %
delta_V_rms(t)=delta_V_rms(t)+(V1-V0)^2; % for each t at a given p, we sum over (V1-V0)^2 in order to eventually calculate delta_V_rms_avg %
delta_V_abs(t)=delta_V_abs(t)+abs(V1-V0); % for each t at a given p, we sum over |V1-V0| in order to eventually calculate delta_V_abs_avg %
delta_V_max(t)=max(abs(V1-V0),delta_V_max(t)); % for each t at a given p, we take the max of |V1-V0| from all the sites in order to eventually calculate delta_V_max_avg %
end
end
end
How do I define input for variables like V which is an LxLxL array?
댓글 수: 5
Adam
2019년 9월 2일
Ah yes, it's a while since I used it. If you enter :Inf as the dimension size then it is allowed to be anything, so
:Inf x :Inf x :Inf
will be a 3d array of any size. You would then validate the actual size somewhere else if you wish to do this. Usually I have a .m wrapper file around mex functions I create that does any input validation I need so that the C or C++ code just works on the general case, unless I can hard-code sizes (e.g. if something should always be a 3x3x3 array)
답변 (1개)
Soumya Paliwal
2021년 4월 9일
1) How do I define a variable to be 3 dimensional array (the only options I see there are: scalar, vector or matrix)?
- To define a 3D input using the MATLAB Coder app, first select the datatype, then select any one of the options provided (scalar, vector, matrix). After specifying the first two dimensions, leave a space and type the third dimension. The app will understand that the variable is a 3D variable.
2) How do I define an array with dimensions equal to a certain variable (that is also an input)?
- If the reference variable is already defined in the workspace, then in the MATLAB Coder app you can specify your variable to be
coder.typeof(<variable_in_workspace>)
- Alternatively, when specifying the type and value of a variable, you can choose 'Define by Example' and choose the reference variable (present in the workspace) from the list generated.
- This will ensure that your input argument is of the same data size and same dimensions as the reference variable.
- For more information on coder.typeof, please refer https://www.mathworks.com/help/fixedpoint/ref/coder.typeof.html
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!