想要将一个向量用作函数的参数,而不必分离它的元素 [英] Want to use a vector as parameter to a function, without having to separate its elements

查看:90
本文介绍了想要将一个向量用作函数的参数,而不必分离它的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用
func(1,2,3,4,5)
调用matlab函数,它可以很好地工作。 / p>

但如果我这样做:
a = [1,2,3,4,5] %(a [1; 2; 3; 4; 5]给出相同的结果)

然后:

func(a)



给了我:

???错误==> func at 11
没有足够的输入参数。



func.m中的第11行是:



> 错误(nargchk(5,6,nargin));


(1),a(2),a(3),a(4),a(1),a (5))



如何使用向量'a'作为函数的参数?我有另一个函数otherfunc(b)返回a,并希望将其输出用作参数,如 func(otherfunc(b))

解决方案

逗号分隔列表
(CSL)可以作为参数列表传递给函数,



你需要的是一个CSL,它是由数组构造的 1,2,3,4,5



它可以使用像这样的单元格数组来生成:

  a = [1,2,3,4,5]; 
c = num2cell(a);
func(c {:});


If I call a matlab function with: func(1,2,3,4,5) it works perfectly.

But if I do: a=[1,2,3,4,5] %(a[1;2;3;4;5] gives same result)

then:

func(a)

gives me:

??? Error ==> func at 11 Not enough input arguments.

Line 11 in func.m is:

error(nargchk(5, 6, nargin));

I notice that this works perfectly:

func(a(1),a(2),a(3),a(4),a(5))

How can I use the vector 'a' as a parameter to a function? I have another function otherfunc(b) which returns a, and would like to use its output as a paramater like this func(otherfunc(b)).

解决方案

Comma-seperated lists (CSL) can be passed to functions as parameter list,

so what you need is a CSL as 1,2,3,4,5 constructed from an array.

It can be generated using cell array like this:

a=[1,2,3,4,5];
c = num2cell(a);
func(c{:});

这篇关于想要将一个向量用作函数的参数,而不必分离它的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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