how to define a function handle if i need to define a function from R^n to R?
4 次查看(过去 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
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
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);
.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!