在 Matlab 中从数组输出?
我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有点困惑你为什么要这么做。当您可以将
pos
作为包含 20 个元素的单个输出返回时,为什么需要 20 个输出呢?不过,也就是说,您可以使用专门命名的变量 varargout 作为最后一个输出变量,并为其分配一个单元格,该单元格的元素将扩展为函数的输出。这是一个例子:
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:如果您想要做的是重新排列数组以将其传递给另一个 Matlab 函数,这里就是。
作为一个变量:
作为 24 个变量:
我强烈推荐第一种选择。
以下是如何使用此单个变量的两个示例。您仍然可以访问其所有部分。
示例 1:取其所有部分的平均值。
示例 2:将变量分成其组成部分。在我们的例子中,在您的工作区中创建了 24 个名为“var1”到“var24”的变量。
希望这有帮助。
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:
As 24 variables:
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.
Example 2: Separate the variable into its component parts. In our case creates 24 variables named 'var1' to 'var24' in your workspace.
Hope this helps.
考虑使用结构体以便从函数返回那么多值。精心选择的字段名称使“返回值”具有自我声明性。
Consider using a structure in order to return that many values from a function. Carefully chosen field names make the "return value" self declarative.