how to define a function handle if i need to define a function from R^n to R?

4 vues (au cours des 30 derniers jours)
San Carlo
San Carlo le 16 Déc 2020
Commenté : James Tursa le 16 Déc 2020
Hello! I'm sorry for my dumb question but i need to define a function f(x) from R^n to R where n is very large (say n=1000) but if I use the command f = @(x) = sum(1/2*x(i,:)^2+x(i,:)) it gives me error if I try to insert a x wich belongs to R^n and it only works if I put a scalar value. What's the correct sintex? How do I define this function?
  1 commentaire
James Tursa
James Tursa le 16 Déc 2020
I am confused about what you want. Can you provide a short example of input and desires output? E.g., what would be the desired output for the following inputs:
x = reshape(1:24,2,3,4);
x = reshape(1:120,2,3,4,5);

Connectez-vous pour commenter.

Réponses (1)

Star Strider
Star Strider le 16 Déc 2020
I am not certain what you are doing or what result you want.
Try these to see which one gives you what you want:
fr = @(x) sum(1/2*x(:).^2+x(:),2); % Use Element-Wise exponentiation (.^) & Force Column Vectors To Sum Across Columns
fc = @(x) sum(1/2*x(:).^2+x(:)); % Use Element-Wise exponentiation (.^) & Force Column Vectors To Sum Down Columns
x = rand(5,1);
yr = fr(rx);
yc = fc(x);
.

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by