将向量传递给没有 num2cell 的 matlab 函数 [英] passing a vector to a matlabfunction WITHOUT num2cell

查看:31
本文介绍了将向量传递给没有 num2cell 的 matlab 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的每次模拟运行,我使用 matlabFunction 命令生成许多 m 文件.这些 m 文件用于生成人工势场.(标量)输入的数量在不同的运行中可能会有所不同(由于我正在模拟的机器人数量),但在运行过程中保持不变.因为每个机器人都有自己的势场函数,所以我必须为每个机器人准备输入向量.我目前正在以下列方式为每个机器人 i 执行此操作:

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{:});

对上述代码的简短说明:我必须通过从矩阵 T 中提取一些值来准备一个传输向量 Tvec.此外,我需要位置向量 Xvec,并将它们传递给梯度函数.如果机器人是领导者",还会添加目的地 xd.gradfun 是一个函数句柄单元,指向我生成的势场函数.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.

我的问题是,由于 num2cell 命令,这种方法非常慢,它占用的时间几乎与实际梯度计算一样多.有没有办法绕过使用 num2cell 命令?我愿意编辑潜在场函数的函数输入,只要这可以从 matlab 脚本完成(即这应该可以自动化而不是手动将逗号分隔的输入更改为基于向量的输入).

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).

推荐答案

我找到了解决问题的方法.对于那些感兴趣的人:可以使用 vars 选项操作 matlabFunction 生成的函数的输入变量,如下所示:

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});

其中 vector1、vector2、vector3 是符号向量.这样,生成的函数文件就会接受向量输入.

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

这篇关于将向量传递给没有 num2cell 的 matlab 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆