在 Matlab 中从数组输出?

发布于 2024-09-16 07:38:56 字数 327 浏览 2 评论 0原文

我有一个包含 20 个项目的数组,我想将它们作为输出,以便我可以将其输入到另一个程序中。

pos = [0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,]

我想用它作为另一个程序的输入

function [lowest1, lowest2, highest1, highest2, pos(1), pos(2),... pos(20)]

我尝试过这个但它不起作用还有其他方法可以做到这一点吗?

I have an array of 20 items long and I would like to make them an output so I can input it into another program.

pos = [0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,]

I would like to use this as inputs for another program

function [lowest1, lowest2, highest1, highest2, pos(1), pos(2),... pos(20)]

I tried this and it does not work is there another way to do this?

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

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

发布评论

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

评论(3

不乱于心 2024-09-23 07:38:57

我有点困惑你为什么要这么做。当您可以将 pos 作为包含 20 个元素的单个输出返回时,为什么需要 20 个输出呢?

不过,也就是说,您可以使用专门命名的变量 varargout 作为最后一个输出变量,并为其分配一个单元格,该单元格的元素将扩展为函数的输出。这是一个例子:

function [lowest1, lowest2, highest1, highest2, varargout] = myfun
  % First set lowest1, lowest2, highest1, highest2, and pos here, then:
  varargout = num2cell(pos);

I'm a little confused why you'd want to do that. Why would you want 20 outputs when you could just return pos as a single output containing 20 elements?

However, that said, you can use the specially named variable varargout as the last output variable, and assign a cell to it, and the elements of the cell will be expanded into outputs of the function. Here's an example:

function [lowest1, lowest2, highest1, highest2, varargout] = myfun
  % First set lowest1, lowest2, highest1, highest2, and pos here, then:
  varargout = num2cell(pos);
迎风吟唱 2024-09-23 07:38:57

如果您想要做的是重新排列数组以将其传递给另一个 Matlab 函数,这里就是。

作为一个变量:

s=unique(pos);
q=[s(1) s(2) s(end-1) s(end) pos];
otherFunction(q);

作为 24 个变量:

s=unique(pos); otherFunction(s(1), s(2), s(end-1), s(end), pos(1), pos(2), pos(3), pos(4), pos(5), pos(6), pos(7), pos(8), pos(9), pos(10), pos(11), pos(12), pos(13), pos(14), pos(15), pos(16), pos(17), pos(18), pos(19), pos(20));

我强烈推荐第一种选择。

以下是如何使用此单个变量的两个示例。您仍然可以访问其所有部分。

示例 1:取其所有部分的平均值。

function otherFunction(varargin)
    myVar=cell2mat(varargin);
    mean(myVar)
end

示例 2:将变量分成其组成部分。在我们的例子中,在您的工作区中创建了 24 个名为“var1”到“var24”的变量。

function otherFunction(varargin)
    for i=1:nargin,
        assignin('base',['var' num2str(i)],varargin{i});
    end
end

希望这有帮助。

If what you're trying to do is re-arrange your array to pass it to another Matlab function, here it is.

As one variable:

s=unique(pos);
q=[s(1) s(2) s(end-1) s(end) pos];
otherFunction(q);

As 24 variables:

s=unique(pos); otherFunction(s(1), s(2), s(end-1), s(end), pos(1), pos(2), pos(3), pos(4), pos(5), pos(6), pos(7), pos(8), pos(9), pos(10), pos(11), pos(12), pos(13), pos(14), pos(15), pos(16), pos(17), pos(18), pos(19), pos(20));

I strongly recommend the first alternative.

Here are two examples of how to work with this single variable. You can still access all of its parts.

Example 1: Take the mean of all of its parts.

function otherFunction(varargin)
    myVar=cell2mat(varargin);
    mean(myVar)
end

Example 2: Separate the variable into its component parts. In our case creates 24 variables named 'var1' to 'var24' in your workspace.

function otherFunction(varargin)
    for i=1:nargin,
        assignin('base',['var' num2str(i)],varargin{i});
    end
end

Hope this helps.

耳根太软 2024-09-23 07:38:57

考虑使用结构体以便从函数返回那么多值。精心选择的字段名称使“返回值”具有自我声明性。

function s = sab(a,b)
  s.a = a;
  s.b = b;

Consider using a structure in order to return that many values from a function. Carefully chosen field names make the "return value" self declarative.

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