can I construct an anonymous function that is a double summation?

1 回表示 (過去 30 日間)
Leo Simon
Leo Simon 2011 年 10 月 31 日
I want to write a double sum as an anonymous function, i.e., sum_i=1^n ( sum_j=1^n (k_i - k_j |) ). Writing the inner sum is trivial, i.e, f = @(x,n,i)sum(|x(i)-x(1:n)),then it's easy enough to sum these, i..e, v(i) = f(x,n,i) ; then sum(v(i)), but I can't figure out how to write the outer loop as an anonymous funct.
Thanks for any help.

採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 10 月 31 日
f = @(i1,j1)sum(reshape(bsxfun(@minus,i1(:),j1(:)'),1,[]))
  1 件のコメント
Leo Simon
Leo Simon 2011 年 10 月 31 日
Thanks andrei! In fact for me, it's even simpler,
f = @(x)sum(reshape(abs(bsxfun(@minus,x,x'),1,[]))

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by