passing a vector to a matlabfunction WITHOUT num2cell

限于喜欢 提交于 2021-01-27 21:03:15

问题


For each of my simulation runs I generate a number of m-files using the matlabFunction command. these m-files are used to generate artificial potential fields. The number of (scalar) inputs may vary among different runs (due to the number of robots I am simulating), but stay constant during the run itself. Because each robot has its own potential field function I have to prepare input vectors for each of these robots. I am currently doing this in the following manner for each robot i:

Tvec = [T(1:i-1,i).' T(i,1:i-1) T(i,i+1:end) T(i+1:end,i).'];
Xvec = reshape(xN_1',1,(J+K)*2);
if sum(leaders==i)
    InputVector = num2cell([Tvec Xvec xd]);
else
    InputVector = num2cell([Tvec Xvec]);
end

fun=gradfun{i};
[gradx,grady] = fun(InputVector{:});

Short explanation of the above code: I have to prepare a transmission vector Tvec by extracting some values from matrix T. Furthermore I need the position vector Xvec, and pass these to the gradient function. If the robot is a 'leader', also the destination xd is added. gradfun is a cell of function handles that point to my generated potential field functions. InputVector is a cell array with scalar entries.

My problem here is that this approach is pretty slow due to the num2cell command, which takes up almost as much time as the actual gradient computation. Is there any way to bypass using the num2cell command? I am willing to edit the function inputs to the potential field functions, as long as this can be done from a matlab script (i.e. this should be possible to automate rather than manually change the comma-separated inputs to a vector-based input).


回答1:


I have found a way to deal with the problem. For those who are interested: it is possible to manipulate the input variables for the function generated by matlabFunction with the vars option as follows:

matlabFunction(symbolic_expression,'file',funname,'vars',{vector1,vector2,vector3});

Where vector1,vector2,vector3 are symbolic vectors. In this way, the function file that is generated will accept vector inputs.




回答2:


I think that you are using num2cell and sending it as InputVector{:} only to generate input arguments for the function. Why don't you pass [Tvec Xvec] (or [Tvec Xvec xd]) to the function and then use that vectors elements as arguments. For example, inside the function, you can write, arg_1=vec(1),...,arg_n=vec(n). I am sure you also know that you can use varargin to differentiate between different no. of input arguments and take actions accordingly.



来源:https://stackoverflow.com/questions/26832620/passing-a-vector-to-a-matlabfunction-without-num2cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!