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

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 31 Okt. 2011
f = @(i1,j1)sum(reshape(bsxfun(@minus,i1(:),j1(:)'),1,[]))
  1 Kommentar
Leo Simon
Leo Simon am 31 Okt. 2011
Thanks andrei! In fact for me, it's even simpler,
f = @(x)sum(reshape(abs(bsxfun(@minus,x,x'),1,[]))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by