如何将多个输出从函数传递到单元格数组 [英] How to pass multiple output from function into cell array

查看:72
本文介绍了如何将多个输出从函数传递到单元格数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下原型的功能

function [bandwidth,density,X,Y,x,y]=kde2d(data,n,MIN_XY,MAX_XY)

基本上,该函数如上所述返回6个输出,其中一些为矢量形式,而另一些为数值形式.如何优雅地将函数的输出传递到1 x 6单元格数组中?

basically the function returns 6 outputs as above, some are in vector form while others are a numerical quantity. How can I elegantly pass the output from the function into a 1 by 6 cell array?

推荐答案

如何

[a{1:6}] = kde2d( data, n, MIN_XY, MAX_XY )

修改:

考虑这个烦人的功能

def foo(n):
  if n == 1:
    return [1, ]
  elif n == 2:
    return [1, ], {'a': 2}
  elif n == 3:
    return [1, ], {'a': 2}, (3, 3, 3)
  return [1, ], {'a': 2}, (3, 3, 3), None

您始终可以将所有输出合并为一个元组:

You can always get all the outputs into a single tuple:

for i in range(1, 5):
  f = foo(i)
  print('got {} outputs: {}'.format(len(f), f))

,此简单循环的输出将是:

and the output of this simple loop would be:

got 1 outputs: [1]
got 2 outputs: ([1], {'a': 2})
got 3 outputs: ([1], {'a': 2}, (3, 3, 3))
got 4 outputs: ([1], {'a': 2}, (3, 3, 3), None)

如果要获取特定的输出:

If you want to get a specific output:

f = foo(2)
f[1]   # accessing the second output, {'a': 2} in this example.

这篇关于如何将多个输出从函数传递到单元格数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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