MATLAB: Can I construct an anonymous function that is a double summation

anonymous functiondouble summation

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.

Best Answer

f = @(i1,j1)sum(reshape(bsxfun(@minus,i1(:),j1(:)'),1,[]))
Related Question