Defining an anonymous function with 3*(N+1) variables
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Anthony Gurunian
am 8 Apr. 2021
Kommentiert: Anthony Gurunian
am 10 Apr. 2021
I need to define a function
f = @(X)...
where X has 3*(N+1) variables in the form: X(:,1), X(:,2), ... X(:,3*(N+1)) . The problem is that the expression for the function is very long. However, there is a pattern which I want to take advantage of.
The function is
Notice that the function can be expressed in terms of summations and products. I want to take advantage of this fact so that I don't have to manually type out the products and sums. Is there any way of doing this using for loops?
4 Kommentare
Cris LaPierre
am 8 Apr. 2021
Perhaps I'm getting hung up on symantics here, but it appears you have a single variable, X. Is it correct to say that your array X has 3*(N+1) columns?
Akzeptierte Antwort
David Hill
am 8 Apr. 2021
f=prod(exp(-beta*(diff(x(1:n+1)).^2+diff(x(n+2:2*n+2)).^2+diff(x(2*n+3:3*n+3)).^2)))*sum((x(1:n+1)-x(1:n+1)').^2+(x(n+2:2*n+2)-x(n+2:2*n+2)').^2+(x(2*n+3:3*n+3)-x(2*n+3:3*n+3)).^2,'all');
7 Kommentare
David Hill
am 9 Apr. 2021
Bearbeitet: David Hill
am 9 Apr. 2021
Is there a different f for each row? If not, I don't understand your equation. If so, then just do a loop with my equation. How can you not know the number of rows? X must be defined before this equation is processed.
for k=1:size(x,1)
f(k)=prod(exp(-beta*(diff(x(k,1:n+1)).^2+diff(x(k,n+2:2*n+2)).^2+diff(k,x(2*n+3:3*n+3)).^2)))*...
sum((x(k,1:n+1)-x(k,1:n+1)').^2+(x(k,n+2:2*n+2)-x(k,n+2:2*n+2)').^2+...
(x(k,2*n+3:3*n+3)-x(k,2*n+3:3*n+3)).^2,'all');
end
Or,
for k=1:size(x,1)
X=reshape(x(k,:),[],3);
f(k)=prod(exp(-beta*(sum(diff(X,1).^2),2)))*sum((X(:,1)-X(:,1)').^2+...
(X(:,2)-X(:,2)').^2+(X(:,3)-X(:,3)').^2,'all');
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Argument Definitions 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!