作为我的机器人学科提供的作业,我应该制作在坐标系之间转换的功能。具体来说,我们应该这样做:
函数参数为
Xo = mySYSTEM1toSYSTEM2(Xi)
,其中:Xi
是矩阵3 * N. 每列显示一个点,列的元素就是 SYSTEM1 中该点的坐标。
我知道转换方程,但我需要matlab语法方面的帮助。我想象的是这样的事情:
%Takes matrix as arguments
% - loops through it and calls mySphericToCarthesian(alpha, beta, ro) for every column
function [Xo] mySphericToCarthesian[Xi]
%Converts one spheric point to another
function [x, y, z] mySphericToCarthesian[alpha, beta, ro]
Matlab似乎不喜欢这样。
如何建立这两个功能,以便我实际上可以从作业本身开始?
答案 0 :(得分:2)
嗯,可能最简单的选择是定义两个不同的功能。在Matlab中,函数重载阴影所有函数只有一个(即一次只能使用一个具有给定名称的函数),这在实践中并不是很有趣。 / p>
但是,如果您只想要一个具有两种不同行为的函数,则可以使用nargin
和nargout
变量来实现。在函数内部,它们指示调用脚本/函数指定的输入和输出的数量。您还可以使用varargin
,将所有输入放在单元格中。
在实践中,这给出了:
function [x, y, z] = mySphericToCarthesian(varargin)
% Put your function description here.
switch nargin
case 1
Xi = varargin{1};
% Do you computation with matrix Xi
% Define only x, which will be used as the only output
case 3
alpha = varargin{1};
beta = varargin{2};
rho = varargin{3};
% Do you computation with alpha, beta and rho
% Define x, y and z for the outputs
end