How do I vectorize a sum involving an anonymous function?

7 ビュー (過去 30 日間)
Norman
Norman 2014 年 8 月 11 日
回答済み: Michael Haderlein 2014 年 8 月 11 日
Hi
I have a sum that involves an anonymous function g of the form
beam = sum_{k=-K}^{k=K} A(k) g(x-c(k))
where A and c are know vectors of length n. I would like to be able to pass beam a vector x of length m ~= n AND vectorize the result. I have tried arrayfun, but get dimension mismatches. Is there any way to implement this without resorting to a loop?
Thank you
Norm

回答 (2 件)

Iain
Iain 2014 年 8 月 11 日
Tricks to try: bsxfun, summation along the right column/row, replicating matrices as needed (repmat)

Michael Haderlein
Michael Haderlein 2014 年 8 月 11 日
So you have a matrix g, right? Then, you don't need to sum by hand but just multiply g*A. Such as
>> x=(1:4)';
>> k=0:5;
>> A=k'.^2;
>> g=x*sqrt(k);
>> g*A
ans =
110.1470
220.2940
330.4410
440.5880
In this example, the first value 110 is g(x(1),k)*A(k) and so on:
>> sum(g(1,:).*A')
ans =
110.1470

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by