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

1 次查看(过去 30 天)
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 个评论
James Tursa
James Tursa 2020-12-16
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);

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2020-12-16
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);
.

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by