[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [newbie] how to avoid a for-loop
From: |
Jean Dubois |
Subject: |
Re: [newbie] how to avoid a for-loop |
Date: |
Thu, 18 Dec 2014 21:33:02 +0100 |
2014-12-18 18:26 GMT+01:00 Marco Atzeri <address@hidden>:
> On 12/18/2014 5:40 PM, Jean Dubois wrote:
>>
>> Can anyone here tell me how I could avoid the for-loop in the
>> following function:
>>
>> function [result] = calp(m)
>> result=[];
>> for k=0:m;
>> result=[result nchoosek(2*m-k,k)];
>> endfor
>>
>> thanks in advance
>> jean
>>
>
> k=[0:m];
> n=2*m-k;
> arrayfun(@nchoosek,n,k)
>
> for m=5
>
> arrayfun(@nchoosek,n,k)
> ans =
>
> 1 9 28 35 15 1
>
>
> Regards
> Marco
thanks Marco,
This is what I made of it:
function [result] = calp(m)
k=[0:m];
n=2*m-k;
result=arrayfun(@nchoosek,n,k)
it works fine, the only thing which I don't understand quite is why
the @ is necessary?
kind regards,
jean