如何将Python元组解包转换为Matlab?

发布于 2024-08-11 05:34:26 字数 872 浏览 4 评论 0 原文

我正在将一些 python 代码翻译成 Matlab,并且想找出将 python 元组解包翻译成 Matlab 的最佳方法是什么。

就本示例而言,Body 是一个类,其构造函数将两个函数作为输入。

我有以下 python 代码:

X1 = lambda t: cos(t)
Y1 = lambda t: sin(t)

X2 = lambda t: cos(t) + 1
Y2 = lambda t: sin(t) + 1

coords = ((X1,Y1), (X2,Y2))
bodies = [Body(X,Y) for X,Y in coords]

被翻译为以下 Matlab 代码

X1 = @(t) cos(t)
Y1 = @(t) sin(t)

X2 = @(t) cos(t) + 1
Y2 = @(t) sin(t) + 1

coords = {{X1,Y1}, {X2,Y2}}
bodies = {}
for coord = coords,
    [X,Y] = deal(coord{1}{:});
    bodies{end+1} = Body(X,Y);
end

,其中 Body 是

classdef Body < handle

    properties
        X,Y
    end

    methods
        function self = Body(X,Y)
            self.X = X;
            self.Y = Y;
        end
    end

end

有没有更好、更优雅的方式来表达 Matlab 中 python 代码的最后一行?

I am translating some python code to Matlab, and want to figure out what the best way to translate the python tuple unpacking to Matlab is.

For the purposes of this example, a Body is a class whose constructor takes as input two functionals.

I have the following python code:

X1 = lambda t: cos(t)
Y1 = lambda t: sin(t)

X2 = lambda t: cos(t) + 1
Y2 = lambda t: sin(t) + 1

coords = ((X1,Y1), (X2,Y2))
bodies = [Body(X,Y) for X,Y in coords]

which is translated to the following Matlab code

X1 = @(t) cos(t)
Y1 = @(t) sin(t)

X2 = @(t) cos(t) + 1
Y2 = @(t) sin(t) + 1

coords = {{X1,Y1}, {X2,Y2}}
bodies = {}
for coord = coords,
    [X,Y] = deal(coord{1}{:});
    bodies{end+1} = Body(X,Y);
end

where Body is

classdef Body < handle

    properties
        X,Y
    end

    methods
        function self = Body(X,Y)
            self.X = X;
            self.Y = Y;
        end
    end

end

Is there a better and more elegant way to express the last line of the python code in Matlab?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2024-08-18 05:34:26

在不知道 Body 是什么的情况下,这是我的解决方案:

bodies = cellfun(@(tuple)Body(tuple{1},tuple{2}), coords);

或者,如果输出必须封装在元胞数组中:

bodies = cellfun(@(tuple)Body(tuple{1},tuple{2}), coords, 'UniformOutput',false);

并且只是为了测试,我尝试了以下方法:

X1 = @(t) cos(t);
Y1 = @(t) sin(t);
X2 = @(t) cos(t) + 1;
Y2 = @(t) sin(t) + 1;

coords = {{X1,Y1}, {X2,Y2}};

%# function that returns a struct (like a constructor)
Body = @(X,Y) struct('x',X, 'y',Y);

%# tuples unpacking
bodies = cellfun(@(tuple)Body(tuple{1},tuple{2}), coords);

%# bodies is an array of structs
bodies(1)
bodies(2)

Without knowing what Body is, this is my solution:

bodies = cellfun(@(tuple)Body(tuple{1},tuple{2}), coords);

or, if the output has to encapsulated in a cell array:

bodies = cellfun(@(tuple)Body(tuple{1},tuple{2}), coords, 'UniformOutput',false);

And just for testing, I tried it with the following:

X1 = @(t) cos(t);
Y1 = @(t) sin(t);
X2 = @(t) cos(t) + 1;
Y2 = @(t) sin(t) + 1;

coords = {{X1,Y1}, {X2,Y2}};

%# function that returns a struct (like a constructor)
Body = @(X,Y) struct('x',X, 'y',Y);

%# tuples unpacking
bodies = cellfun(@(tuple)Body(tuple{1},tuple{2}), coords);

%# bodies is an array of structs
bodies(1)
bodies(2)
娇纵 2024-08-18 05:34:26

看来Amro的答案适用于你。但是,如果您确实不需要或不想创建新的 Body 类,则有一种直接的方法可以使用 STRUCT 命令:

X1 = @(t) cos(t);
Y1 = @(t) sin(t);
X2 = @(t) cos(t) + 1;
Y2 = @(t) sin(t) + 1;
bodies = struct('X',{X1 X2},'Y',{Y1 Y2});

在本例中,数组 bodies 的每个元素是与类对象相反的结构,但您应该能够以大致相同的方式使用它。

It appears that Amro's answer will work for you. However, if you don't really need or want to create a new Body class, there's a straight-forward way to construct an array of structures using the STRUCT command:

X1 = @(t) cos(t);
Y1 = @(t) sin(t);
X2 = @(t) cos(t) + 1;
Y2 = @(t) sin(t) + 1;
bodies = struct('X',{X1 X2},'Y',{Y1 Y2});

In this case, each element of the array bodies is a structure as opposed to a class object, but you should be able to use it in much the same way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文