将可变数量的参数传递给函数 [英] Passing A Variable Number of Arguments into a Function

查看:139
本文介绍了将可变数量的参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中,我有一个可变长度的值单元:

  C = {A1,...,An} 

我应该如何传递这些值并将它们分配给一个可以接收可变数量参数的函数? / p>

  f(A1,...,An)

最终如果不行的话,我应该如何修改这个函数的开始来完成这项工作?

解决方案

您需要将单元格数组转换为以逗号分隔的列表,通过大括号索引,即使用 C {:} $ b $ 重塑函数的示例:

 >> C = {ones(3,4),2,2,3}; 
>> y =重塑(C {:});
>> size(y)%check
ans =
2 2 3


In Matlab, I have a variable length cell of values:

C={A1,...,An}

How should I pass and distribute these values into a function able to receive a variable number of arguments?

f(A1,...,An)

Ultimately if not possible, how should I modify the beginning of this function for making this work?

解决方案

You need to convert the cell array into a comma-separated list via curly-brace indexing, that is, use C{:}.

Example with the reshape function:

>> C = {ones(3,4), 2, 2, 3};
>> y = reshape(C{:});
>> size(y) % check
ans =
     2     2     3

这篇关于将可变数量的参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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