sum of function handle

Dear all, i have the following function:
sum_{1<=i<j<=n}(X_i-X_j)
and i need to write it as function handle. Can anyone help me please. Best regards.

4 Kommentare

Adam
Adam am 15 Dez. 2015
Bearbeitet: Adam am 15 Dez. 2015
Write it as a function first, then the function handle part is trivial.
Trying to go straight from what you have there to a function handle is probably an unnecessarily complicated step unless for some reason you aren't allowed to create actual functions.
Heborvi
Heborvi am 15 Dez. 2015
Bearbeitet: Heborvi am 15 Dez. 2015
Thank you for your comment. I try to write this function but it gives me an error on X (Undefined function X):
function [f]=Target_Distribution(n,q,D)
S=0;
for i=1:n
for j=i+1:n
for k=1:q
S=S+X(i,k)-X(j,k)^2;
end
somme(t)=sqrt(S)-D(i,j);
t=t+1;
S=0;
end
end
Sum_Tot=sum(somme);
f=@(X) Sum_Tot;
Guillaume
Guillaume am 15 Dez. 2015
Your function does not appear to be the same thing at all as the expression you wrote in your question. Why is there a third iterator k, why is there a square ^2 and later a square root sqrt ?
Heborvi
Heborvi am 15 Dez. 2015
Bearbeitet: Heborvi am 15 Dez. 2015
X is a matrix (n x q), each X_i is a vector in R^q. My function is the following:
sum_{1<=i<j<=n} abs( (sqrt(sum_{k=1}^{q} (X(i,k)-X(j,k))^2)-D(i,j)) )
In the first question, i have introduced a simple problem to show how the summation of function handle works.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Guillaume
Guillaume am 15 Dez. 2015

0 Stimmen

"I need to write it as function handle". Why do you need it? As Adam says, work it out as a normal function.
Anyway:
sumdiff = @(x) sum(diff(x(nchoosek(1:numel(x), 2)), [], 2)); %assume x is a vector
The way this works:
  • nchoosek returns all the possible combinations of i and j that satisfy 1<=i<j<=n
  • these combinations are use to index the original array as two columns, x(i) in the first column, x(j) in the second
  • diff calculates the difference between the 2nd and 1st column
  • sum sum all the differences

4 Kommentare

Heborvi
Heborvi am 15 Dez. 2015
Thank you for your answer. In fact the matrix X is unknown therefore i need to write the fuction as function handle.
Guillaume
Guillaume am 15 Dez. 2015
The matrix x may be unknown, that does not preclude writing the function as a regular function.
function s = sumdiff(x)
s = sum(reshape(triu(bsxfun(@minus, x(:), x(:).'), 1), [], 1)); %Matt's answer is more efficient than mine
end
Heborvi
Heborvi am 15 Dez. 2015
Bearbeitet: Heborvi am 15 Dez. 2015
If X is a matrix, this function works? it is possible to include a loop in an handle function? (my algorithm take the fuction as handle function )
Adam
Adam am 15 Dez. 2015
Bearbeitet: Adam am 15 Dez. 2015
You can include anything you like in a handle function if it isn't an anonymous function.
You literally just define your function and then use function handle syntax to create a handle to it. The function could be 5000 lines long if you really want (obviously not advisable!), e.g.
function s = sumdiff(x)
...
end
f = @(x) sumdiff(x)
s = f(x);

Melden Sie sich an, um zu kommentieren.

Matt J
Matt J am 15 Dez. 2015

0 Stimmen

f=@(X) sum( reshape( triu( bsxfun(@minus, X(:),X(:).'),1), [],1) )
Matt J
Matt J am 15 Dez. 2015

0 Stimmen

g=@(X) (length(X):-1:1)*X(:) - sum(cumsum(flip(X)))

3 Kommentare

Heborvi
Heborvi am 15 Dez. 2015
if X is a matrix, flip(X) works?
Adam
Adam am 15 Dez. 2015
doc flip
Matt J
Matt J am 15 Dez. 2015
if X is a matrix, flip(X) works?
I've been assuming -- as your original post assumes -- that X is a vector.

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 15 Dez. 2015

Kommentiert:

am 15 Dez. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by